Last active
June 14, 2017 14:08
-
-
Save flowinho/029733c760a6f937657be6fd2585a4ba to your computer and use it in GitHub Desktop.
progressive image javascript
This file contains 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
// 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