Skip to content

Instantly share code, notes, and snippets.

@SaurabhGarg1
Last active January 20, 2021 05:46
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 SaurabhGarg1/1b6c732c86000216e4cc735545bd16ce to your computer and use it in GitHub Desktop.
Save SaurabhGarg1/1b6c732c86000216e4cc735545bd16ce to your computer and use it in GitHub Desktop.
setup intersection observer and execute logic when intersected
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