Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created July 27, 2021 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihorduchenko/d0546c7608c0a564a476f4b94c11517d to your computer and use it in GitHub Desktop.
Save ihorduchenko/d0546c7608c0a564a476f4b94c11517d to your computer and use it in GitHub Desktop.
Using IntersectionObserver to detect whether element is in viewport or not
const target = document.querySelector('.stickyATCtrigger');
function handleIntersection(entries) {
entries.map((entry) => {
if (entry.isIntersecting) {
// element is in viewport
$('body').removeClass('sticky-addtocart-mob');
} else {
// element is out of viewport
$('body').addClass('sticky-addtocart-mob');
}
});
}
const observer = new IntersectionObserver(handleIntersection);
observer.observe(target);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment