Sha256: 4fe2c88ac61e27a81ad858896dbb44a531534c7b3c582e63deec06709220b727

Contents?: true

Size: 1.67 KB

Versions: 51

Compression:

Stored size: 1.67 KB

Contents

/**
 *  Plugin which is applied on a list of img objects and calls
 *  the specified callback function, only when all of them are loaded (or errored).
 *  @author:  H. Yankov (hristo.yankov at gmail dot com)
 *  @version: 1.0.0 (Feb/22/2010)
 *	http://yankov.us
 */

(function($) {
$.fn.batchImageLoad = function(options) {
	var images = $(this);
	var originalTotalImagesCount = images.size();
	var totalImagesCount = originalTotalImagesCount;
	var elementsLoaded = 0;

	// Init
	$.fn.batchImageLoad.defaults = {
		loadingCompleteCallback: null,
		imageLoadedCallback: null
	}
  var opts = $.extend({}, $.fn.batchImageLoad.defaults, options);

	// Start
	images.each(function() {
		// The image has already been loaded (cached)
		if ($(this)[0].complete) {
			totalImagesCount--;
			if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
		// The image is loading, so attach the listener
		} else {
			$(this).load(function() {
				elementsLoaded++;

				if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);

				// An image has been loaded
				if (elementsLoaded >= totalImagesCount)
					if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
			});
			$(this).error(function() {
				elementsLoaded++;

				if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);

				// The image has errored
				if (elementsLoaded >= totalImagesCount)
					if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
			});
		}
	});

	// There are no unloaded images
	if (totalImagesCount <= 0)
		if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
};
})(jQuery);

Version data entries

51 entries across 51 versions & 7 rubygems

Version Path
showoff-0.9.10.9 public/js/jquery.batchImageLoad.js
showoff-0.9.10.8 public/js/jquery.batchImageLoad.js
showoff-0.9.10.7 public/js/jquery.batchImageLoad.js
showoff-0.9.10.6 public/js/jquery.batchImageLoad.js
showoff-0.9.10.5 public/js/jquery.batchImageLoad.js
showoff-0.9.10.4 public/js/jquery.batchImageLoad.js
showoff-0.9.10.3 public/js/jquery.batchImageLoad.js
showoff-0.9.10.2 public/js/jquery.batchImageLoad.js
showoff-0.9.10.1 public/js/jquery.batchImageLoad.js
showoff-0.9.10 public/js/jquery.batchImageLoad.js
showoff-0.9.9.1 public/js/jquery.batchImageLoad.js
showoff-0.9.9 public/js/jquery.batchImageLoad.js
showoff-0.9.8.1 public/js/jquery.batchImageLoad.js
showoff-0.9.8 public/js/jquery.batchImageLoad.js
showoff-0.9.7.1 public/js/jquery.batchImageLoad.js
showoff-0.9.7 public/js/jquery.batchImageLoad.js
parade-0.10.2 lib/public/js/jquery.batchImageLoad.js
parade-0.10.1 lib/public/js/jquery.batchImageLoad.js
parade-0.10.0 lib/public/js/jquery.batchImageLoad.js
parade-0.9.2 lib/public/js/jquery.batchImageLoad.js