//HTML <img src="img/digital-lazy.jpg" data-src="img/digital.jpg" alt="Computer" class="features__img lazy-img" /> //CSS .lazy-img { filter: blur(20px); } //Lazy loading Images const imgTargets = document.querySelectorAll('img[data-src]'); const loadImage = (entries, observer) => { const [entry] = entries; if (!entry.isIntersecting) return; //Replace src with data-src entry.target.src = entry.target.dataset.src; //Remove lazy-img class after img has been loaded entry.target.addEventListener('load', function () { entry.target.classList.remove('lazy-img'); }); observer.unobserve(entry.target); }; const imgObserver = new IntersectionObserver(loadImage, { root: null, threshold: 0, rootMargin: '200px', }); imgTargets.forEach(img => imgObserver.observe(img));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter