const observer = new IntersectionObserver(entries => {
if (!entries[0].isIntersecting) { // Return if the element isn't on screen
return;
}
}, {
rootMargin: '0px 0px 200px', // margin on the root to check for intersection
threshold: 1
});
observer.observe(<element>);
https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API