@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
22 lines (21 loc) • 777 B
JavaScript
import { isMobile } from '../../esl-utils/environment/device-detector';
let iObserver;
/** ESL Image lazy loading IntersectionObserver instance */
export function getIObserver() {
if (!iObserver) {
iObserver = new IntersectionObserver(function (entries) {
entries.forEach(handleViewport);
}, {
threshold: [0.01], // 0 + 1 are not correctly handled by all browsers
rootMargin: isMobile ? '250px' : '500px' // rootMargin value for IntersectionObserver
});
}
return iObserver;
}
function handleViewport(entry) {
const image = entry.target;
// Check that entry is going to appear in the viewport area
if (entry.isIntersecting || entry.intersectionRatio > 0) {
image.triggerLoad();
}
}