Skip to content

Instantly share code, notes, and snippets.

@Adam-Mould
Created March 20, 2018 08:58
Show Gist options
  • Save Adam-Mould/fa08561b6119c05e4ca7f45ef8a1cde1 to your computer and use it in GitHub Desktop.
Save Adam-Mould/fa08561b6119c05e4ca7f45ef8a1cde1 to your computer and use it in GitHub Desktop.
Basic Lazy Load Images
<img data-src="http://placehold.it/300x300" alt="Example image">
// 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