An Intersection Observer waits (silently) for elements to appear on the screen. It tells you when the element appears. Think of the Intersection Observer API like a sniper waiting for their prey. When the prey appears in view, BAM!
Intersection Observers are better than scroll because it creates less stress on a computer.
How to use the Intersection Observer API
The code for an Intersection Observer looks more complicated than it really is. You need to do four things:
- Create the observer
- Set options (optional)
- Decide what elements to observe
- Run a callback when an element appears on the screen
Creating the observer
Hereโs how you create an Intersection Observer:
const observer = new IntersectionObserver(callback, options)callbackis compulsory. This tells the Intersection Observer what to do when an element appears.optionsis optional. Weโll talk aboutoptionsin a later lesson since theyโre confusing for beginners.
For now, letโs create an empty callback.
function callback() { // Does nothing for now}
const observer = new IntersectionObserver(callback)Observing elements
Intersection Observer lets you observe elements with the observe method. This method takes in a HTML Element.
<ul> <li class="red box"></li> <li class="orange box"></li> <li class="blue box"></li></ul>const observer = new IntersectionObserver(callback)
const element = document.querySelector('li')observer.observe(element)You can observe as many elements as you want. To observe the second element, you add it with the observe method again.
const elements = document.querySelectorAll('li')
// Observes the first elementobserver.observe(elements[0])
// Observes the second elementobserver.observe(elements[1])
// Observes the third elementobserver.observe(elements[2])The callback
An intersection observer callback contains two arguments:
entriesobserver
function callback(entries, observer) { // ...}observer is the intersection observer itself. We donโt need to use observer most of the time.
entries is an array of entry objects. The observer creates an entry when any of three things happen:
- When the element gets observed for the first time.
- When the element enters the screen.
- When the element leaves the screen.
Exploring entries
Letโs go through an example to see when entries get created. Say you want to observe every item in a list.
<ul> <li class="red box"></li> <li class="orange box"></li> <li class="blue box"></li></ul>const observer = new IntersectionObserver(callback)const elements = document.querySelectorAll('li')elements.forEach(element => observer.observe(element))Try and console.log each entry in the entries array.
function callback(entries, observer) { entries.forEach(entry => { console.log(entry) })}When you load the page, you should see three entries.
Letโs push the elements below the screen with a margin-top property.
<ul style="margin-top: 101vh"> <li class="red box"></li> <li class="orange box"></li> <li class="blue box"></li></ul>Reload the page again. The boxes wonโt show up on the screen, but you should still see three entries.
Now scroll down and watch the console. One entry will appear just before a box shows up on the screen.
Entries will also appear just before boxes leave the screen completely.
Entries
Each Intersection Observer entry contains 7 properties. Iโll explain 5 first. Weโll leave the remaining 2 for another lesson:
target: The observed element.boundingClientRect: The observed elementโs bounding rectangle.isIntersecting: Whether the observed element is visible on screen.intersectionRatio: What%the observed element is visible on screen.time: Amount of time that has passed (in milliseconds) since the Intersection Observer was created.
Exercise
Use this HTML and CSS:
<div class="red box"></div><div class="blue box below-fold"></div>.box { width: 300px; height: 100px;}
.red { background-color: red;}
.blue { background-color: blue;}
.below-fold { margin-top: 100vh;}- Create an Intersection Observer.
- Observe both elements.
- Scroll around and notice when entries appear.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson