Skip to content

Instantly share code, notes, and snippets.

@mgmilcher
Created January 24, 2020 04:24
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 mgmilcher/c5152dff2fc0f9df9eb8c599c5f60b20 to your computer and use it in GitHub Desktop.
Save mgmilcher/c5152dff2fc0f9df9eb8c599c5f60b20 to your computer and use it in GitHub Desktop.
function initLazyImages() {
const lazyImages = document.querySelectorAll(".lazy-image");
function onIntersection(imageEntities) {
imageEntities.forEach(image => {
if (image.isIntersecting) {
observer.unobserve(image.target);
image.target.src = image.target.dataset.src;
}
});
}
const observer = new IntersectionObserver(onIntersection);
lazyImages.forEach(image => observer.observe(image));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment