Skip to content

Instantly share code, notes, and snippets.

@mhz-tamb
Created January 23, 2019 07:46
Show Gist options
  • Save mhz-tamb/f222a83a794742cd4dffc54446640f36 to your computer and use it in GitHub Desktop.
Save mhz-tamb/f222a83a794742cd4dffc54446640f36 to your computer and use it in GitHub Desktop.
((document, window) => {
document.addEventListener('DOMContentLoaded', () => {
let images = Array.from(document.querySelectorAll('img[data-src]'));
if (!('IntersectionObserver' in window)) {
images.forEach(image => {
image.src = image.dataset.src;
});
} else {
let observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.intersectionRatio > 0) {
let image = entry.target;
observer.unobserve(image);
image.src = image.dataset.src;
}
});
}, {rootMargin: '50px 0px', threshold: 0.01});
images.forEach(image => observer.observe(image));
}
});
})(document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment