Skip to content

Instantly share code, notes, and snippets.

@amarbalu
Created March 12, 2022 16:05
Show Gist options
  • Save amarbalu/8df5b6d75f081f520ff11b3b457e7c48 to your computer and use it in GitHub Desktop.
Save amarbalu/8df5b6d75f081f520ff11b3b457e7c48 to your computer and use it in GitHub Desktop.
let intersectionObserver;
const trackedElements = new Map();
if (IntersectionObserver) {
intersectionObserver = new window.IntersectionObserver(
(entries, observer) => {
entries.forEach(entry => {
const visibilityHandler = trackedElements.get(entry.target);
if (
visibilityHandler &&
(entry.isIntersecting || entry.intersectionRatio > 0)
) {
visibilityHandler();
}
});
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment