Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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