Back to Home

The Intersection Observer API

The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.

Some more information

Historically, detecting visibility of an element, or the relative visibility of two elements in relation to each other, has been a difficult task for which solutions have been unreliable and prone to causing the browser and the sites the user is accessing to become sluggish. Unfortunately, as the web has matured, the need for this kind of information has grown. Intersection information is needed for many reasons, such as:

Example of Intersection Observer API

To see how it actually works, load this page from mobile screen or resize your browser window to a lower resolution.
random
random

Why do we use it?

Implementing intersection detection in the past involved event handlers and loops calling methods like
Element.getBoundingClientRect()
to build up the needed information for every element affected. Since all this code runs on the main thread, even one of these can cause performance problems. When a site is loaded with these tests, things can get downright ugly.

Browser compatibility

Intersection Observer API - Browser compatibility

More Info