启动CSS动画JavaScript

ato*_*nda 3 html javascript css animation css-animations

如何用js启动CSS动画?第一行(webkitAnimation)有效但其他行则无效.

anim_logo.style.webkitAnimation="threesixty 3s";
anim_logo.style.mozAnimation="threesixty 3s";
anim_logo.style.oAnimation="threesixty 3s";
anim_logo.style.animation="threesixty 3s";
Run Code Online (Sandbox Code Playgroud)

为什么?

实时预览(点击Ninja Star)

Zol*_*oth 5

您最好使用该动画创建一个单独的类,并在需要时将其附加到您的元素:

anim_logo.setAttribute("class", yourAnimationClass);
Run Code Online (Sandbox Code Playgroud)

UPDATE

要删除新添加的类,可以使用延迟函数:

function animateMe() {
    anim_logo.setAttribute( "class", yourAnimationClass );
    setTimeout( function() {
        anim_logo.setAttribute( "class", "" );
    }, 3000);
}
Run Code Online (Sandbox Code Playgroud)

  • @ 11684当你在SO上闲逛时,你会知道有时OP问题的直接答案并不是最好的答案,你可以自由(实际上是鼓励)建议一个更清洁/更好的解决方案. (2认同)