[3 min read] Understanding of JavaScript with the Intersection Observer Web API

Back to IT Blog - The latest and most important web solutions in information technology
Understanding JavaScript with the Intersection Observer Web API - Supermatech.com

[3 min read] Understanding of JavaScript with the Intersection Observer Web API

I came across the Intersection Observer API while learning how Gatsby prefetches links to give the appearance of blazingly fast navigation. I postponed looking into it for a couple of months but finally dove in last weekend.

What Is the Intersection Observer API?

In short, it enables you to detect the visibility of an element, i.e. if it’s in the current viewport, and also the relative visibility of two elements in relationship to each other.

Use Cases

• Lazy loading of images.
• Infinite scrolls.
• Prefetching of links when they appear on the screen (Gatsby does this).
• To detect whether an ad was viewed or not (more tracking).
• To determine whether a user has read an article and to what extent (think medium view vs. read stats).
• To run costly renderings and animations only when they are visible on the screen.

The Technical Stuff

As per usual, the documentation on MDN is great, but I had some trouble figuring out a couple of things and am sharing my notes here for that reason.
At the core of the Intersection Observer API are the following two lines:

1. First, you create an Observer with some options.
2. Then, you ask the Observer to start observing a target.
3. When the desired intersection happens, your callback function is called.

The Options

This was the tricky part to understand. Before going through the settings in options, you should have a firm grasp on the following:

1. Think of root as the outer rectangle, or the rectangle within which you want to observe for an intersection.
2. target is the element which you are watching.
3. threshold gives you the extent to which the overlap should occur.
4. rootMargin is the margin applied to the root before the extent of the intersection is calculated.


Rules of Thumb

1. The API is asynchronous, so use the callback function to do something when the intersection happens.
2. The root element can be decided using the CSS selector.
3. If you specify root: document.querySelector(‘null’), the current viewport of the user will be the root element.
4. Play around with threshold and rootMargin to get the exact behavior you are expecting.

This is pretty much the core concept in Intersection Observer API. For further reading, see MDN’s documentation.


Learning Output

While learning this API, I wanted to see if I could create something useful. So, I wrote just a few lines and created a Chrome extension. The extension updates the URL with the ID of the latest section that is visible on the screen.
So, when you share the updated URL with your friends, they can directly go to the section that you were reading. Pretty cool for a few lines written in less than an hour, eh?

Full Code

The code for this extension is less than 30 lines long.
If you would like to use it, just check this GitHub repo or send me a pull request.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

Share this post

Back to IT Blog - The latest and most important web solutions in information technology