Lazy Load Elements with Intersection Observer

Instead of constantly asking ""Are you on screen yet?"", the Intersection Observer tells the browser: ""Wake me up ONLY when this element enters the screen."" // 1. Define what happens when it enters the screen const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('fade-in'); // Optional: Stop observing once loaded! observer.unobserve(entry.target); } }); }); // 2. Tell it which elements to watch const cards = document.querySelectorAll('.card'); cards.forEach(card => observer.observe(card)); Use this for lazy loading images, trigger-on-scroll animations, and infinite scrolling. Your website will hit 60FPS effortlessly. #WebPerformance #JavaScript #FrontendDev #WebDesign #IntersectionObserver #Coding

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories