IntersectionObserver Guide: Lazy Loading, Infinite Scroll & Scroll Animations in JS & React

You add a scroll listener to reveal cards as they enter the screen. It works on your laptop. On a mid-range phone, scrolling stutters, the page feels heavy, and your performance report blames "long tasks." The listener runs constantly, and every call asks the browser for layout measurements it has to compute on demand.

The IntersectionObserver API fixes this by reversing the question. You stop asking "where is this element right now?" on every scroll event. You tell the browser which element you care about, and it notifies you when that element's visibility changes. This IntersectionObserver guide covers the API from the constructor to its edge cases, then builds three real features:

  • Lazy-loaded images
  • Infinite scroll
  • Scroll-triggered animations

Each feature comes in vanilla JavaScript and as a reusable React hook, with testing and SEO notes along the way.

If you read our ResizeObserver guide, this is its sibling. ResizeObserver tells you when an element's size changes. IntersectionObserver tells you when an element's visibility changes. Many real components use both.

What IntersectionObserver Does (and What It Doesn't)

According to MDN's Intersection Observer API documentation, the API lets you register a callback that runs whenever a target element enters or leaves an intersection with another element, or with the viewport. MDN also lists it as Baseline widely available, meaning it has worked across major browsers since March 2019.