Alt image loaded script


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // $('img.photo',this).imagesLoaded(myFunction)
  2. // execute a callback when all images have loaded.
  3. // needed because .load() doesn't work on cached images
  4.  
  5. // mit license. paul irish. 2010.
  6. // webkit fix from Oren Solomianik. thx!
  7.  
  8. // callback function is passed the last image to load
  9. // as an argument, and the collection as `this`
  10.  
  11.  
  12. $.fn.imagesLoaded = function(callback){
  13. var elems = this.filter('img'),
  14. len = elems.length;
  15.  
  16. elems.bind('load',function(){
  17. if (--len <= 0){ callback.call(elems,this); }
  18. }).each(function(){
  19. // cached images don't fire load sometimes, so we reset src.
  20. if (this.complete || this.complete === undefined){
  21. var src = this.src;
  22. // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
  23. // data uri bypasses webkit log warning (thx doug jones)
  24. this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
  25. this.src = src;
  26. }
  27. });
  28.  
  29. return this;
  30. };

URL: http://gist.github.com/268257

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.