Skip to content

Instantly share code, notes, and snippets.

@mciastek
Last active September 24, 2019 17:34
Show Gist options
  • Save mciastek/173e04844b207b749c7b6bfe5c65d16a to your computer and use it in GitHub Desktop.
Save mciastek/173e04844b207b749c7b6bfe5c65d16a to your computer and use it in GitHub Desktop.
const images = [...document.querySelectorAll('img')];
const onIntersection = (entries, observer) => {
entries.forEach(async (entry) => {
if (entry.isIntersecting) {
try {
// We use loadImage method from previous example
await loadImage(entry.target.src);
} catch (error) {
console.error(error);
} finally {
// When image has been loaded
// stop observing the image
observer.unobserve(entry.target);
}
}
});
};
const observer = new IntersectionObserver(onIntersection);
// Start observing every image
images.forEach((image) => observer.observe(image));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment