Created
June 1, 2017 17:51
-
-
Save shavit/b295e7df324f014a8ae35503719460d4 to your computer and use it in GitHub Desktop.
Preload images on a webpage
This file contains hidden or 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
<html> | |
<img src="" hide> | |
</html> |
This file contains hidden or 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
document | |
.querySelectorAll('img[hide]') | |
.forEach(elm => { | |
let img = document.createElement('img') | |
img.src = elm.src | |
img.onload = (e) => { | |
elm.removeAttribute('hide') | |
} | |
}) |
This file contains hidden or 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{ | |
transition: opacity 1s ease-out; | |
} | |
img[hide]{ | |
opacity: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment