Introduction to the Intersection Observer JavaScript API

Kevin Powell · 187K views · 2019 · 22:11

Open on YouTube

Web developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to trigger code, animations, or style changes as elements scroll into view, as a more efficient alternative to scroll listeners.

Recommended by 4 people across 4 different months, 2020 to 2022.

What it is

Kevin Powell walks through the fundamentals of the JavaScript Intersection Observer API, which fires callbacks when elements enter or leave the viewport instead of relying on scroll event listeners. He covers setting up an observer, the threshold option, and rootMargin, with live coding examples.

What people said

Recommends voiys · 2020-04 · Stack Overflow · on “How to Delay a Javascript Function Until it is in the middle of web page?”

There is a native javascript API for "listetning" where the user currently is on the page called Intersection Observer [LINK] . Basically you set a callback which should execute once the desired content scrolls into view. It's used for all those fancy page animations where cards appear once you start scrolling to the bottom of the page since it's far more efficient than listening on the scroll event. Kevin Powell did a great video [this video] about this topic. Hope it helps!

Recommends EJ Anton Potot · 2021-11 · Stack Overflow · on “How I do change the opacity of text as I scroll, but only when in view of the window?”

you can use intersection observer in javascript. Refer to this video Introduction to the Intersection Observer JavaScript API [this video]

Recommends Artur Müller Romanov · 2021-12 · Stack Overflow · on “How to build an IntersectionObserver in Composition API?”

Figured it out with the help of This great tutorial [this video] by Kevin Powell. Had to put this into my index.vue 's script setup tags: onMounted(() => { const sections = document.querySelectorAll('section') const options = { threshold: 0.5 } const observer = new IntersectionObserver(function(entries, observer) { entries.forEach(entry => { if (entry.isIntersecting) { console.log(entry.target) } }) }, options) sections.forEach(section => { observer.observe(section) }) })

Recommends Rafaa Ferid · 2022-10 · Stack Overflow · on “how to make the menu bar that changes color as it goes down in html”

I think You will have to use Intersection Observer API. [LINK] Much better than listening to scroll event. (it is an API that helps you listen to an element in the DOM (can be an array). and it triggers when that element appears on the screen. then you manipulate the DOM depending on what appears there. Check this tutorial: [this video]