Reputation: 2674
I am working on a rather complex jquery-to be animation which moves various div's based on different trigger events. To simplify the scenario I am trying to solve is:
How should I get around doing something like this?
I am thinking I need to use a trigger/event listener
mechanism, were DIV 2 goes to sleep, and only gets woke up by an event that gets triggered when DIV 1 reaches position B on the screen.
Is this the best mindset I should have? How do I do this using jQuery.
Much appreciated.
Upvotes: 1
Views: 291
Reputation: 2674
I ended up coming up with the following solution which solves my situation:
div1.animate(
{...},
'complete' : function() {
div2.trigger('myStartMove');
}
);
div2.bind('myStartMove', function(ev){
div2.animate(
{...}
);
});
I found it best to use triggers rather than directly call nested animations in the complete callback. Makes my coding a lot cleaner for the complex animation that I have.
Upvotes: 1
Reputation: 488434
The animate function lets you specify a callback that is run when the animation finishes:
$('#div1').animate({...}, 1000, 'linear', function() {
$('#div2').animate({...}, 1000);
});
Upvotes: 6
Reputation: 90022
jQuery has an animate function which accepts a callback. Animate DIV 2 there.
div1.animate({ ... }, 'slow', 'linear', function() {
div2.animate({ ...}, ...);
});
Upvotes: 1