I’ve a HTML aside element on my page and I’ve appended the element some effects (animating). I’m almost done, but I cannot figured it out why the animations for some reason executes disordered…please concentrate on the second function if it is possible because I don’t want to mess up a code that works perfectly
$(document).ready(function(){
$("aside, #top").hover(function(){
$("aside").animate({width:100}, 700);
$("#top").animate({width:100}, 700);
$("#navigation").show();
});
$("aside, #top").mouseleave(function(){
$("aside").animate({width:10}, 700);
$("#top").animate({width:10}, 700);
$("#navigation").hide();
});
The last one
$("#navigation").hide();
Is executed before the other two in the same function. Why?
The reason hide is executed ‘first’ is that all three are being executed at the same time. To order animations you need to use callbacks or chaining. http://api.jquery.com/animate/
The callback will run after the animation has finished. It only appears to run first because
.hidetakes no time at all, but the animations take700msto run.You could also use a timeout if you want the
.hideto occur at a specific point in the animation cycle.or
(psst. “Append” means something closer to concatenate, you don’t append effects to elements, you attach them. And animation operations are not events, so its not an event order problem, its an execution order problem. Cheers.)