Skip to content

Instantly share code, notes, and snippets.

@dallington
Forked from deanhume/intersection-observer.js
Created January 24, 2018 17:51
Show Gist options
  • Select an option

  • Save dallington/603caa7f1517009324696769ef1b4661 to your computer and use it in GitHub Desktop.

Select an option

Save dallington/603caa7f1517009324696769ef1b4661 to your computer and use it in GitHub Desktop.
Intersection Observer - Image lazy load
// Get all of the images that are marked up to lazy load
const images = document.querySelectorAll('.js-lazy-image');
const config = {
// If the image gets within 50px in the Y axis, start the download.
rootMargin: '50px 0px',
threshold: 0.01
};
// The observer for the images on the page
let observer = new IntersectionObserver(onIntersection, config);
images.forEach(image => {
observer.observe(image);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment