Lazy Loading
Lazy Loading
Section titled “Lazy Loading”Introduction
Section titled “Introduction”Use Intersection Observer to defer loading images until they’re about to enter the viewport, saving bandwidth and improving initial page load.
Image Lazy Loading
Section titled “Image Lazy Loading”const imageObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target; img.src = img.dataset.src; // Set real image URL img.removeAttribute('data-src'); imageObserver.unobserve(img); } });}, { rootMargin: '200px' }); // Start loading 200px before visible
document.querySelectorAll('img[data-src]').forEach(img => { imageObserver.observe(img);});HTML Setup
Section titled “HTML Setup”<!-- Use data-src instead of src to prevent loading --><img data-src="real-image.jpg" alt="Lazy loaded image" class="lazy">
<!-- Or use native lazy loading for modern browsers --><img src="real-image.jpg" loading="lazy" alt="Native lazy loading">Summary
Section titled “Summary”- Use
data-srcattribute for real image URL - Swap
data-srctosrcwhen element is near viewport - Use
rootMargin: '200px'to load images before they’re visible - Unobserve after loading to prevent re-triggering