如何通过JavaScript重新触发WebKit CSS动画?

Dav*_*och 74 javascript css html5 animation webkit

所以,我有这个-webkit-animation规则:

@-webkit-keyframes shake {
    0% {
        left: 0;
    }
    25% {
        left: 12px;
    }
    50% {
        left: 0;
    }
    75% {
        left: -12px;
    }
    100% {
        left:0;
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一些CSS定义了我的一些动画规则box:

#box{
    -webkit-animation-duration: .02s;
    -webkit-animation-iteration-count: 10;
    -webkit-animation-timing-function: linear;
}
Run Code Online (Sandbox Code Playgroud)

我可以shake#box这样的:

document.getElementById("box").style.webkitAnimationName = "shake";
Run Code Online (Sandbox Code Playgroud)

但是我以后再也不能动摇了.

这只会震动一次:

someElem.onclick = function(){
    document.getElementById("box").style.webkitAnimationName = "shake";
}
Run Code Online (Sandbox Code Playgroud)

如何在不使用超时或多个动画的情况下通过JavaScript重新触发CSS动画?

Dav*_*och 92

我根据CSS3过渡测试github页面上的源代码和示例找到了答案.

基本上,CSS动画具有animationEnd在动画完成时触发的事件.

对于webkit浏览器,此事件名为" webkitAnimationEnd".因此,为了在调用动画后重置动画,您需要为事件的元素添加事件侦听器animationEnd.

在普通的香草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.
};
Run Code Online (Sandbox Code Playgroud)

和jQuery:

var $element = $('#box').bind('webkitAnimationEnd', function(){
    this.style.webkitAnimationName = '';
});

$('#button').click(function(){
    $element.css('webkitAnimationName', 'shake');
    // you'll probably want to preventDefault here.
});
Run Code Online (Sandbox Code Playgroud)

CSS3转换测试的源代码(如上所述)具有以下support对象,可能有助于跨浏览器的CSS转换,转换和动画.

这是支持代码(重新格式化):

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;
}());
Run Code Online (Sandbox Code Playgroud)

我没有添加代码来检测每个浏览器的"动画"属性.我把这个答案写成了"社区维基"并留给你.:-)

  • 注意:自这个答案以来,github repo中的代码已经改变了; 那里的代码和代码现在完全不同. (2认同)

gil*_*ly3 13

您必须先删除动画,然后再次添加.例如:

document.getElementById("box").style.webkitAnimationName = "";
setTimeout(function ()
{
    document.getElementById("box").style.webkitAnimationName = "shake";
}, 0);
Run Code Online (Sandbox Code Playgroud)

要在没有setTimeout的情况下执行此操作,请在期间删除动画onmousedown,并在onclick以下期间添加

someElem.onmousedown = function()
{
    document.getElementById("box").style.webkitAnimationName = "";
}
someElem.onclick = function()
{
    document.getElementById("box").style.webkitAnimationName = "shake";
}
Run Code Online (Sandbox Code Playgroud)


Puy*_*yol 6

一个简单但有效的选择:

HTML:

<div id="box"></div>
<button id="shake-the-box">Shake it!</button>?
Run Code Online (Sandbox Code Playgroud)

CSS:

#box{
    background: blue;
    margin:30px;
    height:50px;
    width:50px;
    position:relative;
    -moz-animation:shake .2s 0 linear 1; 
    -webkit-animation:shake .2s 0 linear 1; 
}
#box.trigger{
    display:table;
}
@-webkit-keyframes shake {
    0% {
        left: 0;
    }
    25% {
        left: 12px;
    }
    50% {
        left: 0;
    }
    75% {
        left: -12px;
    }
    100% {
        left:0;
    }
}
@-moz-keyframes shake {
    0% {
        left: 0;
    }
    25% {
        left: 12px;
    }
    50% {
        left: 0;
    }
    75% {
        left: -12px;
    }
    100% {
        left:0;
    }
}?
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$('#shake-the-box').click(function(){

  $('#box').toggleClass('trigger');

});?
Run Code Online (Sandbox Code Playgroud)

演示:http:
//jsfiddle.net/5832R/2/

问题:
我不知道它是否适用于Firefox,因为动画似乎不适用于那里......


Deb*_*dez 6

根据https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Tips的建议,删除然后使用requestAnimationFrame添加动画类,以确保渲染引擎处理这两个更改.我认为这比使用setTimeout更干净,并且在前一个游戏完成之前处理重放动画.

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

});

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