Skip to content

Instantly share code, notes, and snippets.

@shavit
Created June 1, 2017 17:51
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 shavit/b295e7df324f014a8ae35503719460d4 to your computer and use it in GitHub Desktop.
Save shavit/b295e7df324f014a8ae35503719460d4 to your computer and use it in GitHub Desktop.
Preload images on a webpage
<html>
<img src="" hide>
</html>
document
.querySelectorAll('img[hide]')
.forEach(elm => {
let img = document.createElement('img')
img.src = elm.src
img.onload = (e) => {
elm.removeAttribute('hide')
}
})
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