-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy-load.js
47 lines (41 loc) · 1.7 KB
/
lazy-load.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
// Usage:
// <img src="you can use a blurred small sized image of the huge-image or use this transparent svg image uri" data-lazy-src="huge-image-path">
// data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 3 2'%3E%3C/svg%3E
// run lazyLoad()
// Dependency - []
// even better way to do lazy loading is with IntersectionObserver api
// Here is a really good article about lazy loading using IntersectionObserver api
// https://css-tricks.com/tips-for-rolling-your-own-lazy-loading/
const lazyLoad = () => {
let lazyloadImages;
let lazyloadThrottleTimeout;
const loadElsInViewPort = () => {
lazyloadImages = document.querySelectorAll('[data-lazy-src]');
let threshold = 300;
let innerHeightOffset = window.innerHeight + threshold;
if (lazyloadThrottleTimeout) {
clearTimeout(lazyloadThrottleTimeout);
}
lazyloadThrottleTimeout = setTimeout(function () {
lazyloadImages.forEach((img) => {
let bounding = img.getBoundingClientRect();
if (bounding.width != 0 || bounding.height != 0 || bounding.x != 0 || bounding.y != 0) {
if (bounding.top >= -(threshold) && bounding.bottom <= innerHeightOffset) {
img.src = img.dataset.lazySrc;
img.removeAttribute('data-lazy-src');
}
}
});
if (lazyloadImages.length == 0) {
document.removeEventListener('scroll', loadElsInViewPort);
window.removeEventListener('resize', loadElsInViewPort);
window.removeEventListener('orientationChange', loadElsInViewPort);
}
}, 20);
}
document.addEventListener('scroll', loadElsInViewPort);
window.addEventListener('resize', loadElsInViewPort);
window.addEventListener('orientationChange', loadElsInViewPort);
loadElsInViewPort();
};
export default lazyLoad;