How to change the duration of a CSS3 keyframes-animation with raw JavaScript

雨燕双飞 提交于 2019-12-10 08:20:59

问题


Is there a way to change the duration of a CSS3 keyframes animation with JavaScript? In CSS you can do that with the animation-duration property:

animation-duration: 1s;

The JavaScript should be raw, i don't want to include jQuery or other JS librarys into my site.


回答1:


You can assign css classes in javascript and put your transition/duration/animation in those css classes Or you can assign your css directly in javascript.

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

for cross browsers we can use o,moz,ms and webkit as prefix.

Example-:

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

EXAMPLE

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

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


来源:https://stackoverflow.com/questions/17766276/how-to-change-the-duration-of-a-css3-keyframes-animation-with-raw-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!