Skip to content

Instantly share code, notes, and snippets.

@AnkurVyas-BTC
Created January 7, 2017 12:15
Show Gist options
  • Select an option

  • Save AnkurVyas-BTC/155b59cf17f56d5f456a393533587ea2 to your computer and use it in GitHub Desktop.

Select an option

Save AnkurVyas-BTC/155b59cf17f56d5f456a393533587ea2 to your computer and use it in GitHub Desktop.
Code for Progressive Image Loading
window.onload = function() {
var placeholder = $('.placeholder');
placeholder.each( function(index) {
// 1: load small image and show it
var smallImgElement = $(this).find('.img-small');
console.log(smallImgElement);
var img = new Image();
img.src = smallImgElement.attr('src');
img.onload = function () {
smallImgElement.addClass('loaded');
};
// 2: load large image
var imgLarge = new Image();
imgLarge.src = $(this).data('large');
imgLarge.onload = function () {
imgLarge.classList.add('loaded');
};
$(this).append(imgLarge);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment