How do I re-trigger a WebKit CSS animation via JavaScript? How do I re-trigger a WebKit CSS animation via JavaScript? javascript javascript

How do I re-trigger a WebKit CSS animation via JavaScript?


I found the answer based on the source code and examples at the CSS3 transition tests github page.

Basically, CSS animations have an animationEnd event that is fired when the animation completes.

For webkit browsers this event is named “webkitAnimationEnd”. So, in order to reset an animation after it has been called you need to add an event-listener to the element for the animationEnd event.

In plain vanilla javascript:

var element = document.getElementById('box');element.addEventListener('webkitAnimationEnd', function(){    this.style.webkitAnimationName = '';}, false);document.getElementById('button').onclick = function(){    element.style.webkitAnimationName = 'shake';    // you'll probably want to preventDefault here.};

and with jQuery:

var $element = $('#box').bind('webkitAnimationEnd', function(){    this.style.webkitAnimationName = '';});$('#button').click(function(){    $element.css('webkitAnimationName', 'shake');    // you'll probably want to preventDefault here.});

The source code for CSS3 transition tests (mentioned above) has the following support object which may be helpful for cross-browser CSS transitions, transforms, and animations.

Here is the support code (re-formatted):

var css3AnimationSupport = (function(){    var div = document.createElement('div'),        divStyle = div.style,        // you'll probably be better off using a `switch` instead of theses ternary ops        support = {            transition:                divStyle.MozTransition     === ''? {name: 'MozTransition'   , end: 'transitionend'} :                // Will ms add a prefix to the transitionend event?                (divStyle.MsTransition     === ''? {name: 'MsTransition'    , end: 'msTransitionend'} :                (divStyle.WebkitTransition === ''? {name: 'WebkitTransition', end: 'webkitTransitionEnd'} :                (divStyle.OTransition      === ''? {name: 'OTransition'     , end: 'oTransitionEnd'} :                (divStyle.transition       === ''? {name: 'transition'      , end: 'transitionend'} :                false)))),            transform:                divStyle.MozTransform     === '' ? 'MozTransform'    :                (divStyle.MsTransform     === '' ? 'MsTransform'     :                (divStyle.WebkitTransform === '' ? 'WebkitTransform' :                 (divStyle.OTransform      === '' ? 'OTransform'      :                (divStyle.transform       === '' ? 'transform'       :                false))))            //, animation: ...        };    support.transformProp = support.transform.name.replace(/([A-Z])/g, '-$1').toLowerCase();    return support;}());

I have not added the code to detect “animation” properties for each browser. I’ve made this answer “community wiki” and leave that to you. :-)


You have to first remove the animation, then add it again. Eg:

document.getElementById("box").style.webkitAnimationName = "";setTimeout(function (){    document.getElementById("box").style.webkitAnimationName = "shake";}, 0);

To do this without setTimeout remove the animation during onmousedown, and add it during onclick:

someElem.onmousedown = function(){    document.getElementById("box").style.webkitAnimationName = "";}someElem.onclick = function(){    document.getElementById("box").style.webkitAnimationName = "shake";}


Following the suggestion from https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Tips, remove and then add the animation class, using requestAnimationFrame to ensure that the rendering engine processes both changes. I think this is cleaner than using setTimeout, and handles replaying an animation before the previous play has completed.

$('#shake-the-box').click(function(){     $('#box').removeClass("trigger");  window.requestAnimationFrame(function(time) {  window.requestAnimationFrame(function(time) {      $('#box').addClass("trigger");    });    });    

});

http://jsfiddle.net/gcmwyr14/5/