Forked from giuliandrimba/jquery.loadimages.js
Last active
December 14, 2015 23:29
-
-
Save thiagocoelho/5165827 to your computer and use it in GitHub Desktop.
This file contains 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 characters
(function($) | |
{ | |
/* | |
* Author: Giulian Drimba & Thiago Coelho | |
* | |
*/ | |
$.fn.loadImages = function(callback, scope) | |
{ | |
var images = $(this).find("img"); | |
var bg_images_elements = $(this).find("*"); | |
for (var i = 0; i < bg_images_elements.length; i++) | |
{ | |
var bg_el = $(bg_images_elements[i]).css("background-image"); | |
if (bg_el !== "none") | |
{ | |
var pattern = /url\("{0,1}([^"]*)"{0,1}\)/; | |
var url = pattern.exec(bg_el)[1]; | |
var img = new Image(); | |
img.src = url; | |
images.push(img); | |
} | |
} | |
var totalImages = images.length; | |
var imagesLoaded = 0; | |
//console.log("totalImages", totalImages); | |
var self = this; | |
if(scope) | |
self = scope; | |
for (var i = 0; i < totalImages; i++) | |
{ | |
var img = images[i]; | |
var simg = $(img); | |
var currImage = new Image(); | |
currImage.src = simg.attr("src"); | |
//console.log(currImage.width); | |
if (currImage.width > 0) | |
{ | |
onLoadImage(); | |
} | |
else | |
{ | |
$(img).load(function(){ | |
// console.log("onLoadImage"); | |
onLoadImage(); | |
}); | |
} | |
} | |
function onLoadImage() | |
{ | |
imagesLoaded++; | |
//console.log("imagesLoaded", imagesLoaded); | |
if(imagesLoaded >= totalImages) | |
{ | |
//console.log("callback function", callback); | |
if(callback) | |
{ | |
callback.call(self,callback); | |
//console.log("callback"); | |
} | |
} | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment