点击后来回运行css动画

时间:2018-03-30 13:56:44

标签: css css3 css-animations keyframe

使用css @keyframes我试图在单击某个元素时附加一些动画。

.animate {
    animation-name: action;
    animation-duration: 2s;
    animation-timing-function: linear;
    background-color: green;
} 

@keyframes action {
    0% {
        background-color: gray; 
    }  

   50% {
       background-color: red;
   }

   100% {
       background-color: green;
   }
}

DEMO

点击时会添加.animate类,该框会从灰色变为绿色。但是现在我想再次点击它时动画回灰色(切换功能)。我尝试使用animation-direction: reverse使用相同的动画,但没有播放动画。动画最初不应播放(我遇到过几次)。有什么建议我怎么能做到这一点?

3 个答案:

答案 0 :(得分:3)

我会考虑使用animation-play-state属性,并使用infinitealternate。我的想法是运行动画并在结束时停止它,然后再次运行它,依此类推:

const div = document.querySelector('.target')
div.addEventListener('click', (e) => {
   div.classList.add('play');
   setTimeout(function() {
      div.classList.remove('play');
   },2000)
})
.target {
  width: 100px;
  height: 100px;
  background-color: gray;
  cursor: pointer;
  animation: action 2s linear alternate infinite;
  animation-play-state:paused;
}
.play {
  animation-play-state:running;
}

@keyframes action {
  0% {
    background-color: gray; 
  }  

  50% {
    background-color: red;
  }

  100% {
     background-color: green;
  }
}
<div class="target">

</div>

答案 1 :(得分:2)

使用动画处理这种开/关更改总是很棘手,并且很难平滑无缝地处理重复的更改。

对于您的情况,我建议将其更改为单个转换。在这种情况下,浏览器处理开/关部分要好得多。要通过过渡实现背景颜色的双重更改,请创建具有3种颜色的渐变,并更改渐变位置:

&#13;
&#13;
const div = document.querySelector('.test')
div.addEventListener('click', (e) => {
   div.classList.toggle('play');
})
&#13;
.test {
  border: solid 1px black;
  margin: 10px;
  height: 100px;
  width: 100px;
  background-image: linear-gradient(gray 0%, gray 20%, blue 40%, blue 60%,
   red 80%, red 100%);
  background-size: 100% 9000%;
  background-repeat: no-repeat;
  background-position: center top;
  transition: background-position .3s;
}

.play {
  background-position: center bottom;
}
&#13;
<div class="test">
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

好,这是我的方法:

  • 保持动画,直到您添加.enabled(防止加载动画)
  • 每次点击都切换一个.active
  • 重要:在.active中定义→不同←动画,即使前后摇动或摆动动画相同,也可以将其定义为副本。 (否则返回时将没有 动画)

const div = document.querySelector('.target')
div.addEventListener('click', (e) => {
  div.classList.add('active');
  div.classList.toggle('play');
})
/* REF https://stackoverflow.com/a/49575979 */
body, html { /* eye candy */
  background: #444; display: flex; min-height: 100vh; align-items: center; justify-content: center;
}

div { /* eye candy */
  width: 200px; height: 100px; border: 12px dashed #888; border-radius: 20px; 
  background: red;
}

/* prevents that there is an animation already on loading */
.active {
  animation: trafficLight 2s linear;
}

.play {
  border: 12px solid white; /* white border == play */
  background: green;
  /* yes, already setting for the way back!
     fun fact: if you do not set a DIFFERENT animation, NO animation whatsoever will happen
    (so also for simple bounce animations, where forth=back would be o.k.
     you do need to clone it... varying time (1.001s) or direction is NOT enough.) */
  animation: trafficLightBack 2s linear;
}

@keyframes trafficLight {
  0% { background-color: green; }  
  50% { background-color: yellow; }  
  100% { background-color: red;}  
}

@keyframes trafficLightBack {
  0% { background-color: red; }  
  50% { background-color: yellow; }  
  100% { background-color: green;}  
}
<div class="target"></div>