-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
111 lines (91 loc) · 3.21 KB
/
main.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
const imageLoader = () => {
let lazyImages = [].slice.call(document.querySelectorAll("img"));
if ("IntersectionObserver" in window) {
let lazyImageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
if (entry.target.dataset.src) {
entry.target.src = entry.target.dataset.src;
}
if (entry.target.dataset.srcset) {
entry.target.srcset = entry.target.dataset.srcset;
}
lazyImageObserver.unobserve(entry.target);
}
});
});
lazyImages.forEach(lazyImage => {
lazyImageObserver.observe(lazyImage);
});
} else {
let active = false;
const checkImages = () => {
lazyImages.forEach(lazyImage => {
if ((lazyImage.getBoundingClientRect().top <= window.innerHeight && lazyImage.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyImage).display !== "none") {
if (lazyImage.dataset.src) {
lazyImage.src = lazyImage.dataset.src;
}
if (lazyImage.dataset.srcset) {
lazyImage.srcset = lazyImage.dataset.srcset;
}
lazyImages = lazyImages.filter(image =>
(image !== lazyImage)
);
if (lazyImages.length === 0) {
document.removeEventListener("scroll", lazyLoad);
window.removeEventListener("resize", lazyLoad);
window.removeEventListener("orientationchange", lazyLoad);
}
}
});
active = false;
};
const lazyLoad = () => {
if (active === false) {
active = true;
setTimeout(() => {
checkImages();
}, 200);
}
};
checkImages();
document.addEventListener("scroll", lazyLoad);
window.addEventListener("resize", lazyLoad);
window.addEventListener("orientationchange", lazyLoad);
}
};
const init = () => {
imageLoader();
const toggle = document.querySelector('.navbar-toggle');
const navmenu = document.querySelector('.navigation');
let navState;
const navStyles = {
on: {
bottom: 0,
opacity: 1
},
off: {
bottom: '100%',
opacity: 0
},
};
const setMenuState = state => {
navState = state;
Object.assign(navmenu.style, navStyles[state]);
}
[...document.querySelectorAll('.navigation .nav li > a')].forEach(node => {
node.addEventListener('click', () => setMenuState('off'));
});
toggle.addEventListener('click', () => {
if ( navState === 'on' ) {
setMenuState('off')
} else {
setMenuState('on');
}
});
};
if (document.readyState === 'complete') {
init();
} else {
window.addEventListener('DOMContentLoaded', init);
}