Created
November 26, 2011 10:37
-
-
Save joachimdoerr/1395434 to your computer and use it in GitHub Desktop.
jquery check: are images loading
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
// 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 | |
}); |
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
// 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