ddstats-server/static/scripts/lazyLoadImages.js

24 lines
602 B
JavaScript
Raw Permalink Normal View History

2021-11-04 22:50:57 +01:00
const observerConfig = {
rootMargin: '0px 0px 50px 0px',
threshold: 0
}
const observer = new IntersectionObserver(
(entries, self) => {
for (const entry of entries) {
if (entry.isIntersecting) {
const element = entry.target
const src = entry.target.getAttribute("data-src")
element.setAttribute("src", src)
self.unobserve(element)
}
}
},
observerConfig
)
const images = document.querySelectorAll('img[data-src]') ?? []
for (const image of images)
observer.observe(image)