6

How can you detect which CSS animation just finished in JavaScript?

The ultimate need is to re-trigger a CSS animation. Due to our HTML hierarchy, we prefer not checking the element's class but instead taking action only when a particular animation ends. If you have a method that allows re-triggering an animation without removing/adding a class, please let us know.

Otherwise... our code:

    page.find( '.button.letter' ).on( 'webkitAnimationEnd', function() {
        $( this ).removeClass( 'tap_animation' );

        console.log( 'Hi: ' + this.style.webkitAnimationName );

        if ( !write_mode() ) {
            do_write( this );
        }
    });

this.style.webkitAnimationName always returns the empty string.

Are we doing something wrong?

We need the code for WebKit browsers, specifically Mobile Safari.

Thanks!

2
  • If you want to loop an animation, that's already supported in CSS3. If that isn't what you want, could you elaborate a bit more? Commented Sep 19, 2012 at 16:11
  • Not to loop an animation, just retrigger it without removing/adding a class. We trigger an animation by adding a class then some undetermined time later, we need to retrigger it. If removing/adding a class is the only method, then we need to solve the question listed here. Commented Sep 19, 2012 at 16:17

2 Answers 2

5

From jQuery you can access the originalEvent object, and, from there, the animationName property:

$('body').on('webkitAnimationEnd', function(e){
    var animName = e.originalEvent.animationName;
    console.log(animName);
});​

(Webkit-only) JS Fiddle demo.

From there, simply use an if to check what the animation name is/was (past-tense, I suppose, given that it ended).

The above updated, to give possibly a better illustration:

$('div').on('webkitAnimationEnd', function(e){
    var animName = e.originalEvent.animationName;
    if (animName == 'bgAnim') {
        alert('the ' + animName + ' animation has finished');
    }
});​

(Webkit-only) JS Fiddle demo.

This demo uses the following HTML:

<div><span>text</span></div>​

And CSS:

@-webkit-keyframes bgAnim {
    0%, 100% {
        color: #000;
        background-color: #f00;
    }
    50% {
        color: #fff;
        background-color: #0f0;
    }
}

@-webkit-keyframes fontSize {
    0%, 100% {
        font-size: 100%;
    }
    50% {
        font-size: 300%;
    }
}

div {
    font-weight: bold;
    -webkit-animation: bgAnim;
    -webkit-animation-duration: 2s;
    -webkit-animation-iteration-count: 2;
}

span {
    font-size: 100%;
    font-weight: bold;
    -webkit-animation: fontSize;
    -webkit-animation-duration: 4s;
    -webkit-animation-iteration-count: 1;
}
Sign up to request clarification or add additional context in comments.

5 Comments

You're too awesome, but you and your 51.4k points already know that. :) You don't happen to know another way to re-trigger CSS animations (after some user action) without adding/removing classes do you?
Why thank you! As for restarting an animation, no; I think (though I've not really explored the subject as yet) that adding/removing/toggling classes is the only way to go, unfortunately.
Thanks, David. We have a related question, which we're hoping you know the answer to as well: stackoverflow.com/questions/12499335/…. If you have a chance, we would love your thoughts!
I've had a look, and posted a potential solution. Though I'm not particularly convinced that it's either a great solution, or any better than the one you already had.
One method (if your animation lends itself to it) is to have an infinite iteration animation and toggle its animation-playstate property between paused and running. (But playstate is sort of buggy right now)
0

An event listener for webkitAnimationEnd should work. Something along the lines of:

    $object.addEventListener('webkitAnimationEnd', function(){
        console.log( 'End Detected' );
    }, false);

1 Comment

Thanks, but how do you detect which animation ended. Multiple animations run against the same object.

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.