Created
October 29, 2019 13:18
-
-
Save tsh-code/5a6cf4303aca594c7467d305c5f941be to your computer and use it in GitHub Desktop.
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 observer = new IntersectionObserverr(onCallbackIntersectionObserver, { | |
threshold: 0, | |
// Begin intersecting 25% of the viewport height before it appears to reduce chance of displaying | |
// unloaded content | |
rootMargin: '0px 0px 25% 0px', | |
}); | |
function onCallbackIntersectionObserver(entries: IntersectionObserverEntry[]) { | |
// Ignore additional entries since there's only one. | |
// Add the logic if you plan to handle multiple entries per intersection observer instance | |
// especially in production code. | |
const entry = entries[0]; | |
observer.unobserve(entry); | |
entry.src = entry.dataset.src; | |
} | |
const elements = document.querySelectorAll('img[data-src]'); | |
elements.forEach(observer.observe); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment