如何使用原始JavaScript更改CSS3关键帧动画的持续时间

no9*_*o92 4 javascript animation css3 css-animations

有没有办法用JavaScript改变CSS3关键帧动画的持续时间?在CSS中,您可以使用animation-duration属性执行此操作:

animation-duration: 1s;
Run Code Online (Sandbox Code Playgroud)

JavaScript应该是原始的,我不想将jQuery或其他JS库包含到我的站点中.

HIR*_*KUR 9

您可以在javascript中分配css类并将转换/持续时间/动画放在那些css类中或者您可以直接在javascript中分配您的CSS.

document.getElementById('your_id').style.animationDuration="1s";
Run Code Online (Sandbox Code Playgroud)

对于跨浏览器,我们可以使用o,moz,ms和webkit作为前缀.

例-:

 document.getElementById('your_id').style.webkitTransitionDuration="1s";
Run Code Online (Sandbox Code Playgroud)

function blink()
        {       
document.getElementById('blink').className = "animated blink_css";
        }
Run Code Online (Sandbox Code Playgroud)

//在css

.animated {
    -webkit-animation-fill-mode:both;
    -moz-animation-fill-mode:both;
    -ms-animation-fill-mode:both;
    -o-animation-fill-mode:both;
    animation-fill-mode:both;
    -webkit-animation-duration:1s;
    -moz-animation-duration:1s;
    -ms-animation-duration:1s;
    -o-animation-duration:1s;
    animation-duration:1s;
}

  @keyframes 'blink' {
   0% { background: rgba(255,0,0,0.5); }
   50% { background: rgba(255,0,0,0); }
   100% { background: rgba(255,0,0,0.5); 
}
//try moz for mozilla,o for opera and webkit for safari and chrome 
    .blink_css {
        -webkit-animation-name: blink;
        -moz-animation-name: blink;
        -o-animation-name: blink;
        animation-name: blink;
    }
Run Code Online (Sandbox Code Playgroud)