views:

5109

answers:

4

Hi,

I am using the jQuery cycle plugin to cycle through some images. These images are all contained inside a DIV. I don't want the cycle plugin to run until after all of the images have loaded, or at least the first 4 or 5 so that an image doesn't appear that isn't loaded yet. Is there an easy way of doing this?

I tried adding a class 'lastImg' to the last img in the div and adding a 'load' event to that e.g.

Markup
<div id='cycle'>
  <img src=''/>
  <img src=''/>
  <img src='' class='lastImg'/>
</div>

JQuery
    $('lastImg').load(function(){
      $('#cycle').cycle({..});
    });

This seems okay in Chrome and FF, admittedly it's a little flakey (sometimes it doesn't work, maybe if the image is in the cache?) and not at all in IE (surprise, surprise...).

Any other suggestions on how I can do this?

+2  A: 

A better way will be to write a function to preload sufficient number of images and then call cycle function. Because how, when and in what order the images in page are loaded may vary by browser or other circumstances.

Here is a great little function at ajaxian that preload all images available in even the stylesheets referenced in page.

TheVillageIdiot
+2  A: 

Markup:

<div id='cycle'>
  <img src='path1' class='preload' />
  <img src='path2' class='preload' />
  <img src='path3' class='preload'/>
</div>

Try this:

var cyclerLoaded = false;
var numberOfPreloaded = 0;

function imageLoaded()
{      
  numberOfPreloaded++;

  if (numberOfPreloaded >= 4 && !cyclerLoaded)
  {
    $('#cycle').cycle({..});
    cyclerLoaded = true;
  }
}

$(document).ready(function()
{      

  $('img.preload').each(function()
  {
    // Get image instance.
    var image = new Image();
    image.src = $(this).attr('src');

    if (image.complete)        
      imageLoaded();        
    else        
      image.onload = imageLoaded;

  });

});

...where the preload class is attached to all images you want to preload.

cdmckay
I tried this but I'm getting the same problem, $('img.preload').load() isn't being fired in IE at all and only first time page is loaded in FF. If I navigate away and come back it's not doing it.
Fermin
Did you try that markup too?
cdmckay
Also, make sure it's in the ready handler. I'll change it to show that.
cdmckay
Hey, yeah i added the class='preload' into my img tags and I had the document.ready() code already for all of the rest of my jQuery stuff. If it's working for you i'll try ripping out all other methods to see if something else is interfering with it.I tried adding a $(window).load() event and it's not fired for IE either!
Fermin
Actually I was just reading the documentation for load and it says this: "For elements, it fires when the target element and all of its content has finished loading. Note: load will work only if you set it before the element has completely loaded, if you set it after that nothing will happen." What might be happening is that, by binding your load events in the ready handler, you're binding them after they have loaded and thus not getting the event. What you should do is something like what this guy mentions: http://www.codingforums.com/archive/index.php/t-83914.html. Basically...
cdmckay
...you get an Image object for that image, and see if the `.complete` property is true. If it is, the image is loaded, if it's not then you add the load handler.
cdmckay
@cdmckay - thank you very much. Worked a treat!
Fermin
+3  A: 

Why not hookup the cycle plugin in document's load event? Document's load event fires when all the artificats have been downloaded. [ready event fires when DOM is available.]

Try

$(document).load(function() { //your cycle plugin hookup } );
SolutionYogi
A: 

One thing that's wrong in your code is you are using $('lastImg'), but it should be $('.lastImg').

Thus, your code will never run at all.

rhalff
Since it was working in FF and Chrome I must have had .lastImg in my actual code and ommited it when I copied it into the question.
Fermin