Last active
January 20, 2021 05:46
-
-
Save SaurabhGarg1/1b6c732c86000216e4cc735545bd16ce to your computer and use it in GitHub Desktop.
setup intersection observer and execute logic when intersected
This file contains 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 imageObserver = new IntersectionObserver((entries, imgObserver) => { | |
entries.forEach((entry) => { | |
if (entry.isIntersecting) { | |
const element = entry.target; | |
element.src = element.getAttribute("dataSrc"); | |
element.removeAttribute("dataSrc"); | |
imgObserver.unobserve(element); | |
} | |
}); | |
}); | |
const imgArr = document.querySelectorAll("img"); | |
imgArr.forEach((item) => { | |
imageObserver.observe(item); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment