Created
January 23, 2012 14:16
-
-
Save mloberg/1663355 to your computer and use it in GitHub Desktop.
onImagesLoad
This file contains hidden or 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
// MooTools | |
var onImagesLoad = function(callback){ | |
var images = 0, | |
check; | |
$$("img").each(function(item, key){ | |
images++; | |
var img = new Image(); | |
img.onload = function(){ | |
images--; | |
}; | |
img.src = item.get("src"); | |
}); | |
check = setInterval(function(){ | |
if(images === 0){ | |
callback(); | |
clearInterval(check); | |
return; | |
} | |
}, 50); | |
}; | |
// jQuery | |
var onImagesLoad = function(callback){ | |
var images = 0, | |
check; | |
$("img").each(function(key){ | |
var item = $(this), | |
img = new Image(); | |
images++; | |
img.onload = function(){ | |
images--; | |
}; | |
img.src = item.attr("src"); | |
}); | |
check = setInterval(function(){ | |
if(images === 0){ | |
callback(); | |
clearInterval(check); | |
return; | |
} | |
}, 50); | |
}; | |
// usage | |
onImagesLoad(function(){ | |
// do something once all images are loaded | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment