Created
July 27, 2021 21:52
-
-
Save ihorduchenko/d0546c7608c0a564a476f4b94c11517d to your computer and use it in GitHub Desktop.
Using IntersectionObserver to detect whether element is in viewport or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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