Skip to content

Instantly share code, notes, and snippets.

@rubinchyk
Created September 21, 2023 07:47
Show Gist options
  • Save rubinchyk/adb87613aa067e17ac4d77b3ec6dd587 to your computer and use it in GitHub Desktop.
Save rubinchyk/adb87613aa067e17ac4d77b3ec6dd587 to your computer and use it in GitHub Desktop.
[Intersection Observer - After scroll to block - do something] Intersection Observer - After scroll to block - do something
const observer = new IntersectionObserver(handleIntersection, { threshold: 0 });
function handleIntersection(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.backgroundColor = "#e74c3c";
} else {
entry.target.style.backgroundColor = "#3498db";
}
});
}
const wrappers = document.querySelectorAll(".wrapper");
wrappers.forEach((entry) => { observer.observe(entry) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment