Created
January 23, 2019 07:46
-
-
Save mhz-tamb/f222a83a794742cd4dffc54446640f36 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
((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