简体   繁体   English

如何暂停和继续播放该动画,并且可以使用纯CSS完成动画?

[英]How can I pause and resume this animation and can it be done with pure CSS?

https://jsfiddle.net/w776Lq1u/ https://jsfiddle.net/w776Lq1u/

HTML 的HTML

<div class="shake">
</div>

CSS 的CSS

  div {
  width: 80px;
  height: 80px;
  background-color: gold;

}

.shake {
  animation: shake 0.82s cubic-bezier(.36,.07,.19,.97) running;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  perspective: 1000px;
}

@keyframes shake {
  10%, 90% {
    transform: translate3d(-1px, 0, 0);
  }

  20%, 80% {
    transform: translate3d(2px, 0, 0);
  }

  30%, 50%, 70% {
    transform: translate3d(-4px, 0, 0);
  }

  40%, 60% {
    transform: translate3d(4px, 0, 0);
  }
}

I need this animation to play once, pause for a few seconds, play again, and repeat this infinitely. 我需要此动画播放一次,暂停几秒钟,再次播放,然后无限重复。

Can this be done using pure CSS? 可以使用纯CSS完成此操作吗?

If not, what's the simplest JS/JQuery solution? 如果没有,最简单的JS / JQuery解决方案是什么?

Thanks. 谢谢。

You just have to speed up animation, add a no animation frame and set animation as infinite. 您只需要加快动画速度,添加无动画帧并将动画设置为无限。

As here : 如这里:

https://jsfiddle.net/w776Lq1u/5/ https://jsfiddle.net/w776Lq1u/5/

 .shake {
      animation: shake 1s cubic-bezier(.36,.07,.19,.97) running infinite;
      transform: translate3d(0, 0, 0);
      backface-visibility: hidden;
      perspective: 1000px;
    }

    @keyframes shake {


      0%, 100% {
        transform: translate3d(0, 0, 0);
      }

      30%, 70% {
        transform: translate3d(-1px, 0, 0);
      }

      35%, 65% {
        transform: translate3d(2px, 0, 0);
      }

      40%, 50%, 60% {
        transform: translate3d(-4px, 0, 0);
      }

      45%,55% {
        transform: translate3d(4px, 0, 0);
      }
    }

Here is an attempt for your requirement. 这是您的要求的尝试。

I have reduced the keyframe percentages to a half and for the remaining 50% time the block is in stationary position. 我将关键帧百分比降低了一半,在剩余的50%时间内,该块处于固定位置。 The animation time has also been name double to the previous value to compensate for the keyframe changes 动画时间也被命名为先前值的两倍,以补偿关键帧更改

You can adjust the delay by following the same concept to a third or quarter or more. 您可以通过遵循相同的概念将延迟调整为三分之一或四分之一或更多。

https://jsfiddle.net/w776Lq1u/4/ https://jsfiddle.net/w776Lq1u/4/

div {
  width: 80px;
  height: 80px;
  background-color: gold;

}

.shake {
  animation: shake 1.64s cubic-bezier(.36,.07,.19,.97) infinite;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  perspective: 1000px;
}

@keyframes shake {
  5%, 45% {
    transform: translate3d(-1px, 0, 0);
  }

  10%, 40% {
    transform: translate3d(2px, 0, 0);
  }

  15%, 25%, 35% {
    transform: translate3d(-4px, 0, 0);
  }

  20%, 30% {
    transform: translate3d(4px, 0, 0);
  }

  50%{
    transform: translate3d(0, 0, 0);
  }
}

Yes this can be done in CSS. 是的,这可以在CSS中完成。 For the delay extend the duration, include translate3d(0,0,0) towards the end and adjust your keyframe percentages so that the non-translated position is part of the animation itself. 为了延长延迟时间,请在结尾处包含translate3d(0,0,0)并调整关键帧百分比,以使非平移位置成为动画本身的一部分。 Then use animation-iteration-count: infinite; 然后使用animation-iteration-count: infinite; so the whole thing repeats. 所以整个过程重复一遍。

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

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