faysalmirmd

ImageLoded JQuery Plugin

Jun 3rd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * jQuery imagesLoaded plugin v2.1.1
  3.  * http://github.com/desandro/imagesloaded
  4.  *
  5.  * MIT License. by Paul Irish et al.
  6.  */
  7.  
  8. /*jshint curly: true, eqeqeq: true, noempty: true, strict: true, undef: true, browser: true */
  9. /*global jQuery: false */
  10.  
  11. ;(function($, undefined) {
  12. 'use strict';
  13.  
  14. // blank image data-uri bypasses webkit log warning (thx doug jones)
  15. var BLANK = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
  16.  
  17. $.fn.imagesLoaded = function( callback ) {
  18.     var $this = this,
  19.         deferred = $.isFunction($.Deferred) ? $.Deferred() : 0,
  20.         hasNotify = $.isFunction(deferred.notify),
  21.         $images = $this.find('img').add( $this.filter('img') ),
  22.         loaded = [],
  23.         proper = [],
  24.         broken = [];
  25.  
  26.     // Register deferred callbacks
  27.     if ($.isPlainObject(callback)) {
  28.         $.each(callback, function (key, value) {
  29.             if (key === 'callback') {
  30.                 callback = value;
  31.             } else if (deferred) {
  32.                 deferred[key](value);
  33.             }
  34.         });
  35.     }
  36.  
  37.     function doneLoading() {
  38.         var $proper = $(proper),
  39.             $broken = $(broken);
  40.  
  41.         if ( deferred ) {
  42.             if ( broken.length ) {
  43.                 deferred.reject( $images, $proper, $broken );
  44.             } else {
  45.                 deferred.resolve( $images );
  46.             }
  47.         }
  48.  
  49.         if ( $.isFunction( callback ) ) {
  50.             callback.call( $this, $images, $proper, $broken );
  51.         }
  52.     }
  53.  
  54.     function imgLoadedHandler( event ) {
  55.         imgLoaded( event.target, event.type === 'error' );
  56.     }
  57.  
  58.     function imgLoaded( img, isBroken ) {
  59.         // don't proceed if BLANK image, or image is already loaded
  60.         if ( img.src === BLANK || $.inArray( img, loaded ) !== -1 ) {
  61.             return;
  62.         }
  63.  
  64.         // store element in loaded images array
  65.         loaded.push( img );
  66.  
  67.         // keep track of broken and properly loaded images
  68.         if ( isBroken ) {
  69.             broken.push( img );
  70.         } else {
  71.             proper.push( img );
  72.         }
  73.  
  74.         // cache image and its state for future calls
  75.         $.data( img, 'imagesLoaded', { isBroken: isBroken, src: img.src } );
  76.  
  77.         // trigger deferred progress method if present
  78.         if ( hasNotify ) {
  79.             deferred.notifyWith( $(img), [ isBroken, $images, $(proper), $(broken) ] );
  80.         }
  81.  
  82.         // call doneLoading and clean listeners if all images are loaded
  83.         if ( $images.length === loaded.length ) {
  84.             setTimeout( doneLoading );
  85.             $images.unbind( '.imagesLoaded', imgLoadedHandler );
  86.         }
  87.     }
  88.  
  89.     // if no images, trigger immediately
  90.     if ( !$images.length ) {
  91.         doneLoading();
  92.     } else {
  93.         $images.bind( 'load.imagesLoaded error.imagesLoaded', imgLoadedHandler )
  94.         .each( function( i, el ) {
  95.             var src = el.src;
  96.  
  97.             // find out if this image has been already checked for status
  98.             // if it was, and src has not changed, call imgLoaded on it
  99.             var cached = $.data( el, 'imagesLoaded' );
  100.             if ( cached && cached.src === src ) {
  101.                 imgLoaded( el, cached.isBroken );
  102.                 return;
  103.             }
  104.  
  105.             // if complete is true and browser supports natural sizes, try
  106.             // to check for image status manually
  107.             if ( el.complete && el.naturalWidth !== undefined ) {
  108.                 imgLoaded( el, el.naturalWidth === 0 || el.naturalHeight === 0 );
  109.                 return;
  110.             }
  111.  
  112.             // cached images don't fire load sometimes, so we reset src, but only when
  113.             // dealing with IE, or image is complete (loaded) and failed manual check
  114.             // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
  115.             if ( el.readyState || el.complete ) {
  116.                 el.src = BLANK;
  117.                 el.src = src;
  118.             }
  119.         });
  120.     }
  121.  
  122.     return deferred ? deferred.promise( $this ) : $this;
  123. };
  124.  
  125. })(jQuery);
Add Comment
Please, Sign In to add comment