const observer = new IntersectionObserver(
// callback
function callback(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
// do something
// lazy load, play video, update sticky nav ...etc
// entry.boundingClientRect
// entry.intersectionRatio
// entry.intersectionRect
// entry.target
}
})
},
// options
{
// null for viewport or a dom element
root: null,
// extends or shrinks the roots capturing frame
rootMargin: '0px 0px 0px 0px',
// percentage of such intersection at which Observer should react
threshold: 0, // accepts values from 0 to 1,
}
)
observer.observe(target)