Skip to content

Instantly share code, notes, and snippets.

@giuliandrimba
Created November 13, 2012 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save giuliandrimba/4067872 to your computer and use it in GitHub Desktop.
Save giuliandrimba/4067872 to your computer and use it in GitHub Desktop.
JavaScript: Load all images inside element
(function($)
{
/*
* Author: Giulian Drimba
*
*/
$.fn.loadImages = function(callback, scope)
{
var images = $(this).find("img");
var totalImages = images.length;
var imagesLoaded = 0;
var self = this;
if(scope)
self = scope;
$(this).find("img").each(function()
{
if ($(this).height() > 0)
{
onLoadImage();
}
else
{
$(this).load(function()
{
onLoadImage();
});
}
});
function onLoadImage()
{
imagesLoaded++;
if(imagesLoaded >= totalImages)
{
if(callback)
callback.call(self,callback);
}
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment