Last active
September 24, 2019 17:34
-
-
Save mciastek/173e04844b207b749c7b6bfe5c65d16a 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 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