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 plete?
// 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')
);
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 plete?
// 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')
);
Share
Improve this question
edited Nov 22, 2011 at 14:52
Rory McCrossan
338k41 gold badges320 silver badges351 bronze badges
asked Nov 22, 2011 at 14:40
Harry FHarry F
1223 silver badges10 bronze badges
4 Answers
Reset to default 7Do 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'));
});
Move your code into a function and pass that function to the callback param of fadeTo
function callback(){
// Then move the image
$('.mosaic_list li:empty', obj)
.random(1)
.html(mosaic_box.find('img')
.addClass('mosaic_last_img')
);
}
// Fade out image to be replaced
mosaic_box.find('img').fadeTo(7000, 0.0, callback)
fadeTo(), as with most animation related methods, accept an optional callback argument which you can use to specify the function to run once the animation is pleted.
To modify your code (untested):
// Fade out image to be replaced
mosaic_box.find('img').fadeTo(7000, 0.0, function() {
// Then move the image
$('.mosaic_list li:empty', obj)
.random(1)
.html(mosaic_box.find('img')
.addClass('mosaic_last_img')
);
});
jQuery.fadeTo() has an optional callBack parameter.
http://api.jquery./fadeTo/
.fadeTo( duration, opacity [, callback] )
callback - A function to call once the animation is plete.
The simplest way to do this is using an anonymous 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');
);
});