2

I would like to know how to have a callback whenever animate is in progress, ie starts the animation.

This is the code I tried:

$('#a').on('click', function() {
  $('#b').animate({opacity: 0}, 1000, function() {
    // animation complete
    $('#c').animate({opacity: 0, 1000};
  });
}

I don't want #c to animate when #b animation is complete.

For example, I want #c to start to animate after 0.5 seconds after #b animation start.

dgilperez
  • 10,368
  • 8
  • 63
  • 90
aboutjquery
  • 748
  • 1
  • 8
  • 20

1 Answers1

0

Just start #c's animation after the required delay using jQuery's .delay() method.

$('#a').on('click', function() {
    $('#b').animate({opacity: 0}, 1000);
    $('#c').delay(500).animate({opacity: 0}, 1000);
});

EDIT

Here's a couple of ways to do something whe both animations are complete.

Crude :

$('#a').on('click', function() {
    $('#b').animate({opacity: 0}, 1000);
    $('#c').delay(500).animate({opacity: 0}, 1000, function() {
        //here, #c's animation is guaranteed to be complete, but also #a's due to the timing/durations.
    });
});

More sophisticated :

$('#a').on('click', function() {
    var promise_b = $('#b').animate({opacity: 0}, 1000).promise();
    var promise_c = $('#c').delay(500).animate({opacity: 0}, 1000).promise();
    $.when(promise_b, promise_c).then(function() {
        //here, both animations are guaranteed to be complete.
    })
});
Roamer-1888
  • 18,902
  • 5
  • 31
  • 44
  • its work, thank you so much :) can i ask something more? is it allow? or i should open a new question? how can i have a callback when #b and #c is finish animation? if ask in comment is not allow, please let me know, i will edit it, thanks :) – aboutjquery Mar 11 '15 at 15:34
  • OK, I have added more, to answer your additional question. – Roamer-1888 Mar 11 '15 at 21:12