Skip to content

Instantly share code, notes, and snippets.

@flowinho
Last active June 14, 2017 14:08
Show Gist options
  • Save flowinho/029733c760a6f937657be6fd2585a4ba to your computer and use it in GitHub Desktop.
Save flowinho/029733c760a6f937657be6fd2585a4ba to your computer and use it in GitHub Desktop.
progressive image javascript
// When loading the site...
window.onload = function() {
// Find the elements that are marked as placeholder and img-small and store their reference
var placeholder = document.querySelector('.placeholder'),
small = placeholder.querySelector('.img-small')
var img = new Image();
img.src = small.src;
img.onload = function () {
small.classList.add('loaded'); // Change it"s opacity to 1.0 to display it immediately
};
var imgLarge = new Image();
imgLarge.src = placeholder.dataset.large;
imgLarge.onload = function () {
imgLarge.classList.add('loaded'); // Display the full-res image once it"s loaded
};
placeholder.appendChild(imgLarge); // Add the loaded image to the div, to actually insert it into the website.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment