Intersection Observer

a web page

#one
#two
0.30
0.30
0.60
0.60
0.30
0.30
0.60
0.60
IntersectionObserver is a browser API for detecting when an element enters or leaves some part of the viewport.
You pass some options like rootMargin and threshold, and a callback that will run for each intersection.
	
		let options = {
click for more rootMargin: "0px 0px 0px 0px",
edit threshold: [0.30,0.60]
}

function callback(entries, observer) {
console.log(entries.length) 2
entries.forEach(entry => {
console.log(entry.target.id) #one #two
console.log(entry.isIntersecting) true false
console.log(entry.intersectionRatio) 0.444 0.000
})
}

let observer = new IntersectionObserver(callback, options)

observer.observe(document.querySelector('#one'))
observer.observe(document.querySelector('#two'))

FAQ