Created
March 20, 2018 08:58
-
-
Save Adam-Mould/fa08561b6119c05e4ca7f45ef8a1cde1 to your computer and use it in GitHub Desktop.
Basic Lazy Load Images
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
<img data-src="http://placehold.it/300x300" alt="Example image"> |
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
// Lazy Load images | |
const images = document.querySelectorAll('img[data-src]'); | |
for (let i = 0; i < images.length; i += 1) { | |
const img = images[i]; | |
img.setAttribute('src', img.getAttribute('data-src')); | |
img.onload = () => img.removeAttribute('data-src'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment