- Learning about intersection observer API again with Kevin Powel - https://www.youtube.com/watch?v=T8EYosX4NOo
IntersectionObserver
detects when a specfic element is interesecting within the viewport. It does not let you detect when one element is intersecting another element.
- Basic boilerplate:
const sectionOne = document.querySelector(".section1")
const options = {
root: null, // the view port
threshold: 0.5, // default 0 - a percent e.g 0.25 = 25% . how much of the element needs to be in view before the IntersectionObserver is triggered.
rootMargin: '0px' // The margin of the viewport. increasing this make the viewport more narrow. works like css eg. '10px 10px' .
};
const observer = new IntersectionObserver((entries, observer)=>{
entries.forEach((entry)=>{
console.log('entry: ', entry.target)
entry.target.classList.toggle('outline')
})
}, options)
observer.observe(sectionOne)