Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created October 29, 2019 13:18
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 tsh-code/5a6cf4303aca594c7467d305c5f941be to your computer and use it in GitHub Desktop.
Save tsh-code/5a6cf4303aca594c7467d305c5f941be to your computer and use it in GitHub Desktop.
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