export function lazyLoad(element, fetchAndRender) {
if (!element) return;
if (!('IntersectionObserver' in window)) {
Promise.resolve().then(fetchAndRender);
return;
}
const obs = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
obs.disconnect();
fetchAndRender();
return;
}
}
}, { rootMargin: '100px' });
obs.observe(element);
}
export function lazyLoadAll(pairs) {
for (const [el, cb] of pairs) lazyLoad(el, cb);
}
window.lazyLoad = lazyLoad;
window.lazyLoadAll = lazyLoadAll;