css - 反向重用 CSS 动画(通过重置状态?)

标签 css animation css-animations reverse

我在 CSS 中使用了两个关键帧动画。一个从左向右移动,另一个使用完全相同的值 - 但相反。

@keyframes moveLeft
{
    from {transform: translate3d(50px, 0, 0);}
    to   {transform: translate3d(0px, 0, 0);}
}


@keyframes moveRight
{
    from {transform: translate3d(0px, 0, 0);}
    to   {transform: translate3d(50px, 0, 0);}
}

但是,我想知道是否可以只使用一个关键帧动画。但是只要我添加 animation-direction: reverse,动画就只播放一次。它可能保存了以前使用过一次的信息。 所以:我能以某种方式重置这些信息吗?或者有没有可能在不同的方向使用一个动画两次? (不使用 JS)

http://jsfiddle.net/vrhfd66x/

最佳答案

不,没有办法单独使用 CSS 来重新启动动画。您必须使用 JavaScript 从元素中删除动画,然后将其重新应用于元素(在延迟后)以使其重新启动。

下面是W3C's CSS3 Animation Spec的内容说(在不同的上下文中,但这一点也适用于这种情况):

Note also that changing the value of ‘animation-name’ does not necessarily restart an animation (e.g., if a list of animations are applied and one is removed from the list, only that animation will stop; The other animations will continue). In order to restart an animation, it must be removed then reapplied.

重点是我的

CSS Tricks Article Chris Coiyer 也表示相同,并提供了一些用于重新启动动画的 JS 解决方案。 (注意这篇文章引用了 Oli 的 dabblet,它声称改变持续时间、迭代次数等属性会使其在 Webkit 上重新启动,但它似乎已经过时,因为它们不再适用于 Chrome)。


总结:

虽然您已经谈到了以下内容,但为了完整起见,我将再次重申:

  • 在元素上应用动画后,它会一直保留在元素上,直到它被移除。
  • UA 会跟踪元素上的动画以及它是否已完成。
  • 当您在 :checked 上应用相同的动画(尽管方向不同)时,用户代理不执行任何操作,因为元素上已经存在该动画。
  • 单击复选框时位置的切换(瞬时)是因为在 :checked 选择器中应用了 transform。动画的存在没有任何区别。

解决方案:

正如您从下面的代码片段中看到的那样,即使使用 JavaScript,通过单个动画实现这一点也非常复杂。

var input = document.getElementsByClassName("my-checkbox")[0];

input.addEventListener('click', function() {
  if (this.checked) {
    this.classList.remove('my-checkbox');
    window.setTimeout(function() {
      input.classList.add('anim');
      input.classList.add('checked');
    }, 10);
  } else {
    this.classList.remove('anim');
    window.setTimeout(function() {
      input.classList.remove('checked');
      input.classList.add('my-checkbox');
    }, 10);
  }
});
input {
  transform: translate3d(50px, 0, 0);
}
.my-checkbox {
  animation: moveLeft 1s;
  animation-direction: reverse;
}
.checked {
  transform: translate3d(0px, 0, 0);
}
.anim{
  animation: moveLeft 1s;
}
@keyframes moveLeft {
  from {
    transform: translate3d(50px, 0, 0);
  }
  to {
    transform: translate3d(0px, 0, 0);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<input type="checkbox" class="my-checkbox">

因此,最佳选择(如果您想坚持使用 CSS 动画)是使用两种不同的动画

或者,您也可以查看 Marcelo 的评论。如果实际用例正是 fiddle 中提供的,则 transition 就足够了,并且不需要 animation。转换本质上可以在正向和反向方向上工作,因此会是一个更安全的选择。

关于css - 反向重用 CSS 动画(通过重置状态?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33347992/

相关文章:

ios - 如何在旋转期间删除 UICollectionView 的淡入淡出动画?

iOS7 UINavigationController pushViewController :animated back to back with animation locks up the main thread

html - CSS 边界半径调制

jquery - 如何获得物体的实际位置?

javascript - 添加内容时 div 位置移动

css - 如何使用 css 制作这样的带 Angular 标签?

jQuery 脚本似乎没有动画

javascript - 如何在特定位置启动 CSS3 动画?

javascript - 如何允许点击通过 div 但仍然对悬停使用react?

html - 仅将 HTML、CSS 网页部署到 Tomcat