forked from nestjs/typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathlazy-load-graphs.js
44 lines (36 loc) · 1.57 KB
/
lazy-load-graphs.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
document.addEventListener('DOMContentLoaded', function() {
var lazyGraphs = [].slice.call(document.querySelectorAll('[lazy]'));
var active = false;
var lazyLoad = function() {
if (active === false) {
active = true;
setTimeout(function() {
lazyGraphs.forEach(function(lazyGraph) {
if (
lazyGraph.getBoundingClientRect().top <= window.innerHeight &&
lazyGraph.getBoundingClientRect().bottom >= 0 &&
getComputedStyle(lazyGraph).display !== 'none'
) {
lazyGraph.data = lazyGraph.getAttribute('lazy');
lazyGraph.removeAttribute('lazy');
lazyGraphs = lazyGraphs.filter(function(image) { return image !== lazyGraph});
if (lazyGraphs.length === 0) {
document.removeEventListener('scroll', lazyLoad);
window.removeEventListener('resize', lazyLoad);
window.removeEventListener('orientationchange', lazyLoad);
}
}
});
active = false;
}, 200);
}
};
// initial load
lazyLoad();
var container = document.querySelector('.container-fluid.modules');
if (container) {
container.addEventListener('scroll', lazyLoad);
window.addEventListener('resize', lazyLoad);
window.addEventListener('orientationchange', lazyLoad);
}
});