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,716
  • 8
  • 68
  • 96
aboutjquery
  • 838
  • 2
  • 11
  • 22
  • a callback is by definition something that runs after something finishes. This may help though. http://stackoverflow.com/questions/1251300/how-to-run-two-jquery-animations-simultaneously – DevDonkey Mar 11 '15 at 15:19
  • thanks :) is it about the queue()? i still dont know what is the queue() effect, i should read more tutorial, thank you :) – aboutjquery Mar 11 '15 at 15:42
  • I hope this is what the OP, kind of poorly, tries to ask. – dgilperez Mar 11 '15 at 16:05

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
  • 19,138
  • 5
  • 33
  • 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