Rara scrolls her tablet and Bagas logs every entry a real IntersectionObserver hands over: your root, rootMargin and threshold, each target’s ratio, and a…
Why it matters
Lazy-loaded images, infinite scroll, scroll-spy, "appear on scroll" animations: they all use this. The old way, a scroll listener plus getBoundingClientRect() every frame, is wasteful and janky. The observer is worked out by the browser itself and only calls you when something changes.
The key idea
The callback runs when a line is crossed, not on every scroll
Common mistakes
Forgetting to observe() each target Making the observer is not enough: call io.observe(el) for EVERY element you want watched.
Forgetting disconnect() / unobserve() An observer that is never cleaned up lives on and holds its elements, especially in an SPA. In React, return () => io.disconnect() from useEffect; for lazy-loading, unobserve() the targets already loaded.
The callback never fires though the element clearly shows If root is an element that is not an ancestor of the target, they will never intersect. Use the scrolling element that wraps the targets, or root: null for the viewport.
Try it in this playground
Scroll slowly and watch each block’s ratio on the board Entries arrive as the ratio crosses 0, 0.5 and 1, not at every scrolled pixel.
Set threshold to 1 A report only comes when a block is fully visible, and when it starts to leave.
Use a positive bottom rootMargin, like 0px 0px 150px 0px A block is reported in before it really shows. That is the preload trick.
Type the rootMargin 10 with no unit The constructor throws a SyntaxError: rootMargin must be px or %.