7

I have a problem where I want to fade out an image, then move the image to an empty <li> container. However, the image doesn't fade, instead it just seems like the fade is being overridden by the move, using html().

Below is what I'm trying to do. Is there a way I can force the move to wait for the fade to complete?

// Fade out image to be replaced
mosaic_box.find('img').fadeTo(7000, 0.0)

// Then move the image
$('.mosaic_list li:empty', obj)
    .random(1)
    .html(mosaic_box.find('img')
        .addClass('mosaic_last_img')
    );
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Harry F
  • 122
  • 3
  • 10

4 Answers4

7

Do the move in the callback from the fadeTo function:

mosaic_box.find('img').fadeTo(7000, 0.0, function() { 
    $('.mosaic_list li:empty', obj)
     .random(1)
     .html(mosaic_box.find('img').addClass('mosaic_last_img'));
});
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
  • Cheers @Richard D - only issue is that I now fade the image out, move it to an empty
  • , but my script no longer fades a new image in. I realise this is probably beyond the scope of my original question, but is there anything you can think of, just based on your amendment to my code, that might cause this?
  • – Harry F Nov 22 '11 at 15:00
  • Ah, got it fixed - i needed to wrap my next block of code, executing the fadeIn, inside the same anonymous function. – Harry F Nov 22 '11 at 15:36
  • Yeah, everything that you want to happen after the fadeOut has to happen inside the callback. – Richard Dalton Nov 22 '11 at 15:41