Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created February 23, 2017 03:32
Show Gist options
  • Save alextanhongpin/790325c0fb38895a9bdac7608dfd977d to your computer and use it in GitHub Desktop.
Save alextanhongpin/790325c0fb38895a9bdac7608dfd977d to your computer and use it in GitHub Desktop.
/*
```html
<img data-src="/path/to/image.jpg" alt="">
```
```css
img {
opacity: 1;
transition: opacity 0.3s;
}
img[data-src] {
opacity: 0;
}
```
*/
[].forEach.call(document.querySelectorAll('img[data-src]'), function(img) {
img.setAttribute('src', img.getAttribute('data-src'));
img.onload = function() {
img.removeAttribute('data-src');
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment