Skip to content

Instantly share code, notes, and snippets.

@aFarkas
Last active August 15, 2016 08:50
Show Gist options
  • Save aFarkas/15de1cea3acd6602a71e to your computer and use it in GitHub Desktop.
Save aFarkas/15de1cea3acd6602a71e to your computer and use it in GitHub Desktop.
/**
* How to:
* $('div.container').imagesLoaded(function(){console.log('all images loaded in .container');});
* In case you need to support IE8, you need to use HTML5shiv **and** need to modify jQuery the following way:
* https://github.com/jquery/jquery/commit/a9533893b9e5e9a248139f5794c5d6099382cf14
*/
(function($){
'use strict';
$.fn.imagesLoaded = (function(){
var imageLoaded = function (img, cb, delay){
var timer;
var isReponsive = false;
var $parent = $(img).parent();
var $img = $('<img />');
var srcset = $(img).attr('srcset');
var src = $(img).attr('src');
var onload = function(){
$img.off('load error', onload);
clearTimeout(timer);
cb();
};
if(delay){
timer = setTimeout(onload, delay);
}
$img.on('load error', onload);
if($parent.is('picture')){
$parent = $parent.clone();
$parent.find('img').remove().end();
$parent.append($img);
isReponsive = true;
}
if(srcset){
$img.attr('srcset', srcset);
if(!isReponsive){
$img.appendTo(document.createElement('div'));
}
isReponsive = true;
} else if(src){
$img.attr('src', src);
}
if(isReponsive && !window.HTMLPictureElement){
if(window.respimage){
window.respimage({elements: [$img[0]]});
} else if(window.picturefill){
window.picturefill({elements: [$img[0]]});
} else if(src){
$img.attr('src', src);
}
}
};
return function(cb){
var i = 0;
var $imgs = $('img', this).add(this.filter('img'));
var ready = function(){
i++;
if(i >= $imgs.length){
cb();
}
};
$imgs.each(function(){
imageLoaded(this, ready);
});
return this;
};
})();
})(jQuery);
@hatsumatsu
Copy link

Handy script! But I noticed that it screws responsive images using srcset and sizes because it ignores the sizes attribute. This leads to the largest source being loaded. Works fine when the sizesvalue is added before applying the srcset to the <img> element created.
https://gist.github.com/hatsumatsu/b946fee531aa32a17808

@CHEWX
Copy link

CHEWX commented Aug 15, 2016

This doesn't seem to work in IE11 ? Can you replicate ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment