5

How would I use jQuery to determine when a CSS animation for an element has ended and then apply a new style to that element? Here is my non-functional code below.

$("#icon").jQuery.Event("webkitAnimationEnd", function(event){
    ('#icon').css('opacity', '1');
}); 
2
  • Are you trying to apply this globally without having to assign it for individual animations? Commented Jun 5, 2011 at 0:54
  • I actually need to assign it to individual animations :/ Commented Jun 9, 2011 at 21:39

4 Answers 4

14

I think actually what you are looking for is the .bind() method:

$("#icon").addClass('thisClassWillApplyMyCSSAnimation').bind('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e){
    //DO WHATEV
    $('#icon').css('opacity', '1');
}); 
Sign up to request clarification or add additional context in comments.

Comments

3

You can use one()

$("#icon").one('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) {
    //Your css
    $('#icon').css('opacity', '1');
}); 

Comments

0

If I understand what you mean, you want to animate a css property and define a callback to execute when the animation ended. You should use the .animate() function.

ex:

$('#clickme').click(function() {
  $('#book').animate({
    opacity: 0.25,
  }, 5000, function() {
    // Animation complete.
  });
});

The following script will animate the opacity from its current value to 0.25. this will take 5000 milliseconds. The last function will be called after the animation completes.

Here the JQuery page about that: http://api.jquery.com/animate/

JQuery's great :)

4 Comments

Thanks, but the thing is that I'm already taking care of the animation with CSS3, I just want jQuery to know when that is done. It may seem unnecessary but I really need to use CSS to do the animation :)
Hi Tim, just use the jquery animate method instead of CSS animation, that way its cross browser compatible, and you can use the callback method above.
Yah except jQuery is slow as hell on mobile devices. I agree with the person who posted this, we need to know how to add a style to something once the webkit animation has finished...
this technique is orders of magnitude slower then using css
-1

Most (if not all) animations define a callback for once the activity has completed.

For instance, assuming you want to slideDown() the #MyDiv object and once that's complete, change the opacity of the #icon:

$("#MyDiv").slideDown("[speed]", function()
{
  $("#icon").css("opacity", "1");
});

// [speed] = "slow", "normal", "fast" or a integer value defining milliseconds

I haven't tested that, but it should work...

1 Comment

Stay on topic and answer his question. This is completely wrong because you aren't addressing the topic of how jquery can talk webkit animations events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.