-
-
Save kottenator/960033 to your computer and use it in GitHub Desktop.
Revisions
-
kottenator revised this gist
May 7, 2011 . 1 changed file with 35 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,31 +1,44 @@ /** * Execute a callback when all images have loaded. * needed because .load() doesn't work on cached images * * Usage: * $('img.photo').imagesLoaded(myFunction) * -> myFunction() { this == [jQuery collection of 'img'] } * * $('img.photo').imagesLoaded(myFunction, myContext) * -> myFunction() { this == myContext } * * Adds: * + Image error counts as image load. * + Empty 'img' set fires callback. * * MIT license. kottenator. 2011 */ $.fn.imagesLoaded = function(callback, context) { var elems = this.filter('img'), len = elems.length, blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; context = context || elems; function countdown() { if (this.src != blank) { if (--len <= 0) callback.call(context, this); } } elems.each(function() { var src = this.src; this.src = blank; $(this).one('load error', countdown); this.src = src; }); if (!elems.length) callback.call(context); return this; }; -
paulirish revised this gist
Apr 29, 2011 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,17 +11,18 @@ $.fn.imagesLoaded = function(callback){ var elems = this.filter('img'), len = elems.length, blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; elems.bind('load',function(){ if (--len <= 0 && this.src !== blank){ callback.call(elems,this); } }).each(function(){ // cached images don't fire load sometimes, so we reset src. if (this.complete || this.complete === undefined){ var src = this.src; // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f // data uri bypasses webkit log warning (thx doug jones) this.src = blank; this.src = src; } }); -
paulirish revised this gist
Jun 30, 2010 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,8 @@ $.fn.imagesLoaded = function(callback){ if (this.complete || this.complete === undefined){ var src = this.src; // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f // data uri bypasses webkit log warning (thx doug jones) this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; this.src = src; } }); -
paulirish revised this gist
May 29, 2010 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,6 @@ $.fn.imagesLoaded = function(callback){ this.src = src; } }); return this; }; -
paulirish revised this gist
Apr 8, 2010 . 1 changed file with 10 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,19 +3,25 @@ // needed because .load() doesn't work on cached images // mit license. paul irish. 2010. // webkit fix from Oren Solomianik. thx! // callback function is passed the last image to load // as an argument, and the collection as `this` $.fn.imagesLoaded = function(callback){ var elems = this.filter('img'), len = elems.length; elems.bind('load',function(){ if (--len <= 0){ callback.call(elems,this); } }).each(function(){ // cached images don't fire load sometimes, so we reset src. if (this.complete || this.complete === undefined){ var src = this.src; // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f this.src = '#'; this.src = src; } }); }; -
paulirish created this gist
Jan 4, 2010 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ // $('img.photo',this).imagesLoaded(myFunction) // execute a callback when all images have loaded. // needed because .load() doesn't work on cached images // mit license. paul irish. 2010. // callback function is passed the last image to load // as an argument, and the collection as `this` $.fn.imagesLoaded = function(callback){ var elems = this.filter('img'), len = elems.length; elems.bind('load',function(){ if (--len <= 0){ callback.call(elems,this); } }).each(function(){ // cached images don't fire load sometimes, so we reset src. if (this.complete || this.complete === undefined){ this.src = this.src; } }); }