Is it possible to detect when all images are loaded via a jQuery event?
Ideally, there should be a
$(document).idle(function()
{
}
maybe this plugin may be useful: http://www.farinspace.com/jquery-image-preload-plugin/
$( "img.photo" ).load(function() {
$(".parrentDiv").css('height',$("img.photo").height());
// very simple
});
window.load = function(){}
Its fully supported by all browsers, and it will fire an event when all images are fully loaded.
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
Waiting for all images to load...
I found the anwser to my problem with jfriend00 here jquery: how to listen to the image loaded event of one container div? .. and "if (this.complete)"
Waiting for the all thing to load and some possibly in cache !.. and i have added the "error" event...
it's robust across all browsers
$(function() { // Wait dom ready
var $img = $('img'), // images collection
totalImg = $img.length,
waitImgDone = function() {
totalImg--;
if (!totalImg) {
console.log($img.length+" image(s) chargée(s) !");
}
};
$img.each(function() {
if (this.complete) waitImgDone(); // already here..
else $(this).load(waitImgDone).error(waitImgDone); // completed...
});
});
Demo : http://jsfiddle.net/molokoloco/NWjDb/
There's a note on the ahpi.imgload.js plugin at the moment saying that it is currently broken, and to try this gist instead: https://gist.github.com/797120/7176db676f1e0e20d7c23933f9fc655c2f120c58
I created my own script, because I found many plugins to be quite bloated, and I just wanted it to work the way I wanted. Mine checks to see if each image has a height (native image height). I've combined that with the $(window).load() function to get around the issues of caching.
I've code commented it quite heavily, so it should be interesting to look at, even if you don't use it. It works perfectly for me.
Without further ado, here it is:
imgLoad.js script