IntersectionObserver tells you when an element enters or leaves the viewport. The classic use is lazy-loading images, but it covers infinite scroll, scroll-driven animations, and visibility analytics with one API.
Tune the rootMargin to start work before the element is visible — '200px' is a common value for image preloading. Use threshold arrays for finer-grained tracking.
Disconnect the observer when you no longer need it, especially in long-running SPA pages — accumulated observers leak DOM references and slow scrolling.