Intersection Observer efficiently detects when elements enter or leave the viewport.
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
}
});
});
observer.observe(document.querySelector(".lazy"));
Use this for lazy loading images or infinite scroll.