Last active
July 18, 2018 12:24
-
-
Save st-f/c05e8713f5932967c80f2e3797b92b9d 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
<script> | |
function isInView(el) { | |
return (el.getBoundingClientRect().top >= 0) && (el.getBoundingClientRect().bottom <= window.innerHeight); | |
} | |
function checkVisibility() { | |
var images = document.getElementsByTagName('img'); | |
for (var i=0; i<images.length; i++) { | |
if(images[i].getAttribute('data-src') && images[i].getAttribute('src') == null) { | |
if (isInView(images[i])) | |
images[i].setAttribute('src', images[i].getAttribute('data-src')); | |
} | |
} | |
} | |
window.addEventListener('scroll', checkVisibility); | |
window.addEventListener('load', checkVisibility); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment