Skip to content

Instantly share code, notes, and snippets.

@joachimdoerr
Created November 26, 2011 10:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joachimdoerr/1395434 to your computer and use it in GitHub Desktop.
Save joachimdoerr/1395434 to your computer and use it in GitHub Desktop.
jquery check: are images loading
// deferred obj callback
function loadImg(selector) {
var dfd = $.Deferred();
$(selector).load(function() { dfd.resolve(); });
return dfd.promise();
}
// more elements
$.when(
loadImg('#img1'),
loadImg('#img2'),
loadImg('#img3'))
.then(function() {
//do stuff, all images are loaded
});
// one element
$.when(loadImg('#img1')).then(function() {
//do stuff, the image is loaded
});
// imgesload callback
$.fn.imagesLoaded=function(callback){var $images=this.find('img'),len=$images.length,_this=this,blank='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';function triggerCallback(){callback.call(_this,$images)}function imgLoaded(){if(--len<=0&&this.src!==blank){setTimeout(triggerCallback);$images.unbind('load error',imgLoaded)}}if(!len){triggerCallback()}$images.bind('load error',imgLoaded).each(function(){if(this.complete||this.complete===undefined){var src=this.src;this.src=blank;this.src=src}});return this};
// function isImageLoad
function isImageLoad(){
var objImagesContainer = $('#alpha').hide();
function showContainer(objThisContainer,$images) {
objThisContainer.show();
}
objImagesContainer.imagesLoaded(function($images) {
showContainer(this,$images);
});
};
// initialize
isImageLoad();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment