Skip to content

Instantly share code, notes, and snippets.

@st-f
Last active July 18, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st-f/c05e8713f5932967c80f2e3797b92b9d to your computer and use it in GitHub Desktop.
Save st-f/c05e8713f5932967c80f2e3797b92b9d to your computer and use it in GitHub Desktop.
<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