From 07e1db99a745aa875266966261d421b28e05a86d Mon Sep 17 00:00:00 2001 From: vista-man <524715@vistacollege.nl> Date: Thu, 31 Oct 2024 12:12:25 +0100 Subject: [PATCH] swa --- js/lazyloading.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/js/lazyloading.js b/js/lazyloading.js index 8a7fbf6..79a0a82 100644 --- a/js/lazyloading.js +++ b/js/lazyloading.js @@ -17,4 +17,23 @@ function lazyLoad() { }); } +function lazyLoad() { + const lazyImages = document.querySelectorAll('img[data-src]'); + + const observer = new IntersectionObserver((entries) => { + entries.forEach((entry) => { + if (entry.isIntersecting) { + const lazyImage = entry.target; + lazyImage.src = lazyImage.dataset.src; + lazyImage.classList.remove('lazy'); + observer.unobserve(lazyImage); + } + }); + }); + + lazyImages.forEach((lazyImage) => { + observer.observe(lazyImage); + }); +} + window.addEventListener('load', lazyLoad); \ No newline at end of file