-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
80 lines (71 loc) · 1.75 KB
/
image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @classdesc Represents a FabrImage component.
* @extends FabrCoreComponent
*/
fbr.FabrImage = class extends fbr.FabrCoreComponent {
constructor() {
super();
this.componentName = "FabrImage";
this.componentStyleClass = "fabr-image";
this.selector = "[fabr-image]";
this.eventMap = {};
this.animateHelper = new fbr.FabrHelperAnimate();
this.animateHelper.init(this);
}
/**
* Render the image.
* @param {HTMLElement} image The image.
*/
render(image) {
const settings = this.getElementSettings(image);
image.width = image.width;
image.height = image.height;
image.realSrc = image.src;
if (settings.length === 0) {
return;
}
if (settings.includes("managed")) {
this.addInternalEventListener(
document,
"scroll",
"manageImageOnScroll",
image
);
this.addInternalEventListener(window, "resize", "manageImage", image);
this.addInternalEventListener(
window,
"orientationchange",
"manageImage",
image
);
}
}
/**
* Manage the image on scroll.
* @param {Event} event The event.
* @param {HTMLElement} image The image.
*/
manageImageOnScroll(event, image) {
clearTimeout(image.scrollTimeout);
image.scrollTimeout = setTimeout(() => {
this.manageImage(event, image);
}, 100);
}
/**
* Manage the image.
* @param {HTMLElement} image The image.
*/
manageImage(_, image) {
let process = false;
if (!this.isElementInViewport(image.parentNode)) {
process = true;
}
if (process) {
image.src = "";
image.style.visibility = "hidden";
} else {
image.src = image.realSrc;
image.style.visibility = "visible";
}
}
};