-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyload.js
184 lines (184 loc) · 7.28 KB
/
lazyload.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*jslint browser: true*/
/*jshint esversion: 6 */
/* global window, document */
var LazyLoad = (function () {
"use strict";
const ARRAY = Array;
const OBJECT = Object;
const WINDOW = window;
const DOCUMENT = document;
const listen = "addEventListener";
const unlisten = "removeEventListener";
const setTimeout = WINDOW.setTimeout;
const forEach = function (array, callback) {
return ARRAY.prototype.forEach.call(array, callback);
};
const hasProperty = function (object, property) {
return OBJECT.prototype.hasOwnProperty.call(object, property);
};
const isInArray = function (array, value) {
return (array.indexOf(value) >= 0);
};
const getLoadedNodes = function (tag, attribute) {
const nodes = DOCUMENT.getElementsByTagName(tag);
const loaded = [];
forEach(nodes, function (node) {
const property = node.getAttribute(attribute);
if (property) {
loaded.push(property);
}
});
return loaded;
};
const Public = function () {
return;
};
Public.prototype.script = function (queue) {
const loaded = getLoadedNodes("script", "src");
const indexes = OBJECT.keys(queue);
function Load() {
if (indexes.length === 0) {
return;
}
const index = indexes.shift();
const script = queue[index];
if (hasProperty(script, "src") && !isInArray(loaded, script.src)) {
loaded.push(script.src);
const node = DOCUMENT.createElement("script");
const attributes = OBJECT.keys(script);
forEach(attributes, function (attribute) {
node[attribute] = script[attribute];
});
DOCUMENT.body.appendChild(node);
}
setTimeout(Load);
}
setTimeout(Load);
};
Public.prototype.link = function (queue) {
const loaded = getLoadedNodes("link", "href");
const Load = function () {
const fragment = DOCUMENT.createDocumentFragment();
forEach(OBJECT.keys(queue), function (index) {
const link = queue[index];
const hasHref = hasProperty(link, "href");
const isLoaded = isInArray(loaded, link.href);
if (hasHref && !isLoaded) {
loaded.push(link.href);
const node = DOCUMENT.createElement("link");
const attributes = {
rel: "stylesheet",
type: "text/css"
};
forEach(OBJECT.keys(attributes), function (attribute) {
const isSet = hasProperty(link, attribute);
if (!isSet) {
link[attribute] = attributes[attribute];
}
});
forEach(OBJECT.keys(link), function (attribute) {
node[attribute] = link[attribute];
});
fragment.appendChild(node);
}
});
DOCUMENT.head.appendChild(fragment);
};
const AnimationFrame = WINDOW.requestAnimationFrame;
if (AnimationFrame) {
AnimationFrame.call(WINDOW, function () {
setTimeout(Load);
});
} else if (DOCUMENT.readyState === "loading") {
DOCUMENT[listen]("DOMContentLoaded", Load, true);
} else {
setTimeout(Load);
}
};
Public.prototype.onSight = function (selector) {
if (!selector) {
return;
}
const Observ = function() {
const nodeList = DOCUMENT.querySelectorAll(selector);
const isObserver = hasProperty(WINDOW, "IntersectionObserver");
const setNodeSource = function (node) {
const applyDataset = function (element) {
forEach(["srcset", "src", "data"], function (property) {
if (hasProperty(element.dataset, property)) {
element[property] = element.dataset[property];
}
});
};
if (node.children.length) {
forEach(OBJECT.keys(node.children), function (index) {
applyDataset(node.children[index]);
});
if (isInArray(["VIDEO", "AUDIO"], node.tagName)) {
node.load();
}
} else {
applyDataset(node);
}
};
if (isObserver) {
const ObserverConstructor = WINDOW.IntersectionObserver;
const Observer = new ObserverConstructor(function(list) {
forEach(list, function(entry) {
if (entry.isIntersecting) {
setNodeSource(entry.target);
Observer.unobserve(entry.target);
}
});
});
forEach(nodeList, function(node) {
Observer.observe(node);
});
} else {
let running = false;
const nodeArray = ARRAY.prototype.slice.call(nodeList);
const isVisible = function (element) {
const clientRect = element.getBoundingClientRect();
const style = WINDOW.getComputedStyle(element);
const inViewport = (
(clientRect.top <= WINDOW.innerHeight) &&
clientRect.bottom >= 0
);
return (inViewport && style.display !== "none");
};
const onSight = function() {
if (running) {
return;
}
running = true;
setTimeout(function () {
forEach(nodeArray, function(element, index) {
if (!isVisible(element)) {
return;
}
setNodeSource(element);
nodeArray.splice(index, 1);
if (nodeArray.length === 0) {
WINDOW[unlisten]("load", onSight, true);
WINDOW[unlisten]("resize", onSight);
WINDOW[unlisten]("orientationchange", onSight);
DOCUMENT[unlisten]("scroll", onSight, true);
}
});
running = false;
}, 200);
};
WINDOW[listen]("load", onSight, true);
WINDOW[listen]("resize", onSight);
WINDOW[listen]("orientationchange", onSight);
DOCUMENT[listen]("scroll", onSight, true);
}
};
if (DOCUMENT.readyState === "loading") {
DOCUMENT[listen]("DOMContentLoaded", Observ, true);
} else {
setTimeout(Observ);
}
};
return new Public();
}());