Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

SaurabhGarg1

🎯
Focusing
View GitHub Profile
@SaurabhGarg1
SaurabhGarg1 / setupInterSectionObserver.js
Last active Jan 20, 2021
setup intersection observer and execute logic when intersected
View setupInterSectionObserver.js
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);
}
});
});