-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathlazyLoad.js
50 lines (46 loc) · 1.53 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
/*
* Includes utility methods for lazy loading media
*/
/**
* Check if IntersectionObserver is supported
* @return {boolean} true if window.IntersectionObserver is defined
*/
export function isIntersectionObserverSupported() {
// Check that 'IntersectionObserver' property is defined on window
return typeof window === "object" && window.IntersectionObserver;
}
/**
* Check if native lazy loading is supported
* @return {boolean} true if 'loading' property is defined for HTMLImageElement
*/
export function isNativeLazyLoadSupported() {
return typeof HTMLImageElement === "object" && HTMLImageElement.prototype.loading;
}
/**
* Calls onIntersect() when intersection is detected, or when
* no native lazy loading or when IntersectionObserver isn't supported.
* @param {Element} el - the element to observe
* @param {function} onIntersect - called when the given element is in view
*/
export function detectIntersection(el, onIntersect) {
try {
if (isNativeLazyLoadSupported() || !isIntersectionObserverSupported()) {
// Return if there's no need or possibility to detect intersection
onIntersect();
return;
}
// Detect intersection with given element using IntersectionObserver
const observer = new IntersectionObserver(
(entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
onIntersect();
observer.unobserve(entry.target);
}
});
}, {threshold: [0, 0.01]});
observer.observe(el);
} catch (e) {
onIntersect();
}
}