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

时间:2013-07-20 20:11:30

标签: javascript css3 animation css-animations

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

animation-duration: 1s;

JavaScript应该是原始的,我不想在我的网站中包含jQuery或其他JS库。

1 个答案:

答案 0 :(得分:8)

您可以在javascript中指定css类,并将转换/持续时间/动画放在那些css类中。或者您可以直接在javascript中指定css。

document.getElementById('your_id').style.animationDuration="1s";

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

示例 - :

 document.getElementById('your_id').style.webkitTransitionDuration="1s";

EXAMPLE

function blink()
        {       
document.getElementById('blink').className = "animated blink_css";
        }

//在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;
    }