Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was playing with this and I created a slightly different solution. Instead of pushing images onto an array when they are loaded, you push them all onto an array in the loop, then in the load event you remove them from the array and call a 'finished' function. It checks if the images array is empty, and if it is then it clears up and shows the content.</p> <pre><code>var html = '&lt; animg src="images/test.jpg" alt="test" /&gt;'; // not allowed to post images... $('div.content').hide().html(html); // preload images // define usr variables object $.usrvar = {}; // array of loaded images' urls $.usrvar.images = []; // initially no images $.usrvar.hasimages = false; $('div.content img').each(function() { // if we're here then this content has images.. $.usrvar.hasimages = true; // set this image's src to a var var src = this.src; // add this image to our images array $.usrvar.images.push(src); // callback when image has finished loading $(this).load(function(){ var index = $.usrvar.images.indexOf(src); $.usrvar.images.splice(index,1); finish_loading(); }); // create a new image var img = new Image(); // set our new image's src img.src = src; }); if(!$.usrvar.hasimages) finish_loading(); function finish_loading() { if($.usrvar.hasimages) { if($.usrvar.images.length &gt; 0) return; } $('div.content').slideDown('normal'); } </code></pre> <p>Edit: Looking at Julien's post, his method is better. My method works in a similar way but like the original solution keeps track of images by an array of srcs rather than just a count (which is more efficient).</p> <p>Edit 2: well I thought it was a better solution, but it seems it doesnt work for me. Maybe something to do with the load event getting called too close to each other. Sometimes it will work but sometimes it will hang when loading images, and the image counter never reaches zero. I've gone back to the method in my post above.</p> <p>Edit 3: It appears it was the setTimeout that was causing the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload