Skip to content

Instantly share code, notes, and snippets.

@mciastek
Last active August 19, 2021 01:08
Show Gist options
  • Save mciastek/26411dca5b7f004381267f8a885f2c00 to your computer and use it in GitHub Desktop.
Save mciastek/26411dca5b7f004381267f8a885f2c00 to your computer and use it in GitHub Desktop.
const intersectionObserver = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
// element is in viewport, do the stuff
// it's good to remove observer,
// if you don't need it any more
observer.unobserve(entry.target);
}
});
});
// get your elements, by class name '.js-item'
const elements = [...document.querySelectorAll('.js-item')];
// start observing your elements
elements.forEach((element) => intersectionObserver.observe(element));
@mciastek
Copy link
Author

mciastek commented Sep 1, 2019

Move the observer.unobserve(entry.target); inside the condition otherwise only the first two in view get animated and nothing else.

Thanks! I changed it in the gist, along with isIntersecting.

@denisinla
Copy link

Move the observer.unobserve(entry.target); inside the condition otherwise only the first two in view get animated and nothing else.

Thanks! I changed it in the gist, along with isIntersecting.

👍 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment