简体   繁体   English

使用JavaScript控制CSS3关键帧动画

[英]Controling CSS3 keyframe animation with JavaScript

I have a simple keyframe animation: 我有一个简单的关键帧动画:

animation: blink-truck-lights .4s 8s 10s steps(2) 2 forwards ;
@keyframes blink-truck-lights{
 from{background-position: 0px 0;}

 to{background-position: 0px -250px;}
}

Here is the JS part: 这是JS部分:

   setInterval(function(){
 $('#truck').addClass('blink-truck-lights');       

 },500);
  setInterval(function(){
 $('#truck').removeClass('blink-truck-lights');       

 },800);

Now, I would need it to play over a specified time interval, about 8 seconds. 现在,我需要它在指定的时间间隔(约8秒)内播放。 How to accomplish this, maybe with adding and removing class with the animation syntax was what came to my mind. 我想到了如何实现这一点,也许是通过动画语法添加和删除类。 But I tried setInterval, and it added the class, but when I created another interval for removing the class, the animation just wouldn't start. 但是我尝试了setInterval,它添加了类,但是当我创建另一个删除类的间隔时,动画就不会开始。

You can do it by pure css also.. 您也可以通过纯CSS实现。

#id {
  -webkit-animation: NAME-YOUR-ANIMATION 8s infinite; /* Safari 4+ */
  -moz-animation:    NAME-YOUR-ANIMATION 8s infinite; /* Fx 5+ */
  -o-animation:      NAME-YOUR-ANIMATION 8s infinite; /* Opera 12+ */
  animation:         NAME-YOUR-ANIMATION 8s infinite; /* IE 10+ */
} 

LINK 链接

UPDATE 2: ------------------------------------------------------------------------- 更新2: ----------------------------------------------- --------------------------

Javascript answer Javascript答案

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

setInterval(function(){
blink();
},8000)

IN CSS---> 在CSS --->

      @keyframes 'blink' {
     //your code for animation
                         }

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

        .animated {
            -webkit-animation-duration:8s;
            -moz-animation-duration:8s;
            -ms-animation-duration:8s;
            -o-animation-duration:8s;
             animation-duration:8s;
                  }

UPDATE 3: ------------------------------------------------------------------------- 更新3: ----------------------------------------------- --------------------------

.paused{
    -webkit-animation-play-state:paused;
    -moz-animation-play-state:paused;
    -o-animation-play-state:paused; 
    animation-play-state:paused;
}

Just add and remove this class whenever you need .Hope this helps.Cheers!!! 只需在需要时添加和删除此类,希望对您有所帮助。

This is one way of doing this, other that animationEnd or animationStart events. 这是执行此操作的一种方式,除了animationEnd或animationStart事件之外。 Just toggle the class on the desired element, and set the intreval at which you want the animation to start over again. 只需在所需元素上切换类,然后设置希望动画重新开始的时间间隔即可。

setInterval(function(){$('#truck').toggleClass('blink-truck-lights')},10000);

Now, the truck lights will blink every 10 seconds. 现在,卡车指示灯将每10秒闪烁一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM