如何仅在循环之间在悬停时暂停CSS动画?

时间:2020-04-08 20:20:28

标签: html css animation

我正在创建具有文本内容的纯CSS滑块。

它基于https://codepen.io/cassidoo/pen/MyaWzp

html,
body {
  font-family: 'Droid Serif', serif;
}

h1 {
  font-size: 60px;
  text-align: center;
}

.content-slider {
  width: 100%;
  height: 360px;
}

.slider {
  height: 320px;
  width: 680px;
  margin: 40px auto 0;
  overflow: visible;
  position: relative;
}

.mask {
  overflow: hidden;
  height: 320px;
}

.slider ul {
  margin: 0;
  padding: 0;
  position: relative;
}

.slider li {
  width: 680px;
  height: 320px;
  position: absolute;
  top: -325px;
  list-style: none;
}

.slider .quote {
  font-size: 40px;
  font-style: italic;
}

.slider .source {
  font-size: 20px;
  text-align: right;
}

.slider li.anim1 {
  animation: cycle 15s linear infinite;
}

.slider li.anim2 {
  animation: cycle2 15s linear infinite;
}

.slider li.anim3 {
  animation: cycle3 15s linear infinite;
}

.slider li.anim4 {
  animation: cycle4 15s linear infinite;
}

.slider li.anim5 {
  animation: cycle5 15s linear infinite;
}

.slider:hover li {
  animation-play-state: paused;
}

@keyframes cycle {
  0% {
    top: 0px;
  }
  4% {
    top: 0px;
  }
  16% {
    top: 0px;
    opacity: 1;
    z-index: 0;
  }
  20% {
    top: 325px;
    opacity: 0;
    z-index: 0;
  }
  21% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
  50% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
  92% {
    top: -325px;
    opacity: 0;
    z-index: 0;
  }
  96% {
    top: -325px;
    opacity: 0;
  }
  100% {
    top: 0px;
    opacity: 1;
  }
}

@keyframes cycle2 {
  0% {
    top: -325px;
    opacity: 0;
  }
  16% {
    top: -325px;
    opacity: 0;
  }
  20% {
    top: 0px;
    opacity: 1;
  }
  24% {
    top: 0px;
    opacity: 1;
  }
  36% {
    top: 0px;
    opacity: 1;
    z-index: 0;
  }
  40% {
    top: 325px;
    opacity: 0;
    z-index: 0;
  }
  41% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
  100% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
}

@keyframes cycle3 {
  0% {
    top: -325px;
    opacity: 0;
  }
  36% {
    top: -325px;
    opacity: 0;
  }
  40% {
    top: 0px;
    opacity: 1;
  }
  44% {
    top: 0px;
    opacity: 1;
  }
  56% {
    top: 0px;
    opacity: 1;
    z-index: 0;
  }
  60% {
    top: 325px;
    opacity: 0;
    z-index: 0;
  }
  61% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
  100% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
}

@keyframes cycle4 {
  0% {
    top: -325px;
    opacity: 0;
  }
  56% {
    top: -325px;
    opacity: 0;
  }
  60% {
    top: 0px;
    opacity: 1;
  }
  64% {
    top: 0px;
    opacity: 1;
  }
  76% {
    top: 0px;
    opacity: 1;
    z-index: 0;
  }
  80% {
    top: 325px;
    opacity: 0;
    z-index: 0;
  }
  81% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
  100% {
    top: -325px;
    opacity: 0;
    z-index: -1;
  }
}

@keyframes cycle5 {
  0% {
    top: -325px;
    opacity: 0;
  }
  76% {
    top: -325px;
    opacity: 0;
  }
  80% {
    top: 0px;
    opacity: 1;
  }
  84% {
    top: 0px;
    opacity: 1;
  }
  96% {
    top: 0px;
    opacity: 1;
    z-index: 0;
  }
  100% {
    top: 325px;
    opacity: 0;
    z-index: 0;
  }
}
<h1>Pure CSS3 Text Carousel</h1>

<div class="content-slider">
  <div class="slider">
    <div class="mask">
      <ul>
        <li class="anim1">
          <div class="quote">Hello, this is a quote from a person.</div>
          <div class="source">- Person</div>
        </li>
        <li class="anim2">
          <div class="quote">Hello, this is a quote from another person.</div>
          <div class="source">- Another person</div>
        </li>
        <li class="anim3">
          <div class="quote">Hello, this is a quote from an animal.</div>
          <div class="source">- Animal</div>
        </li>
        <li class="anim4">
          <div class="quote">Hello, this is a quote from a plant.</div>
          <div class="source">- Plant</div>
        </li>
        <li class="anim5">
          <div class="quote">How do ya like that.</div>
          <div class="source">- Cassidy</div>
        </li>
      </ul>
    </div>
  </div>
</div>

一切正常,但是悬停暂停功能会将动画暂停在当前位置,该位置可以在周期的中间。这是正确的明显行为,但不适合我的滑块。 下图显示了大约50%暂停的周期:

This image shows a CSS cycle paused at about 50%

是否有某种方法可以使活动周期在暂停之前完成? 如果不是,是否有一些技巧可以在周期内禁用指针事件?

更新: 我现在可以在Chrome和Firefox中使用它,但是Safari似乎不接受关键帧中的指针事件。

我添加了一个背景来确认空闲实例的时间。 实际上,我在.slider div上为此添加了一个额外的关键帧,并添加了步骤开始来删除过渡。当然,该动画也会在悬停时暂停,否则,动画将在悬停滑块之后异步运行。 https://jsfiddle.net/8f1senmt/

任何人都知道为什么Safari无法正常运行吗?仅仅是限制还是其他设置搞砸了?

1 个答案:

答案 0 :(得分:1)

我对您的代码有点打扰,如果添加这些行,它将起作用。 我将pointer-events: none添加到了滑块。仅在特定帧中,它将设置为auto

我将背景色赋予了可悬停的帧中的滑块,然后它将停止。唯一的问题是:如果您用鼠标移动,只会识别悬停。因此,如果鼠标在滑块中,但不移动,将无法工作。

您还需要调整时间范围,因为背景颜色不在正确的范围内。

.slider {
  height: 320px;
  width: 680px;
  margin: 40px auto 0;
  overflow: visible;
  position: relative;
  pointer-events: none;
  animation: slidercycle 15s linear infinite;
}
.slider:hover{
  animation-play-state: paused;
}

@keyframes slidercycle {
  0% {
    pointer-events: none;
  }
  19% {
    pointer-events: auto;
    background-color: transparent;
  }
  20% {
    pointer-events: auto;
    background-color: red;
  }
  21% {
    pointer-events: none;
    background-color: transparent;
  }
  39%{
    pointer-events: none;
    background-color: transparent;
  }
  40% {
    pointer-events: auto;
    background-color: blue;
  }
  41% {
    pointer-events: none;
    background-color: transparent;
  }
  59%{
    pointer-events: none;
    background-color: transparent;
  }
  60% {
    pointer-events: auto;
    background-color: green;
  }
  61% {
    pointer-events: none;
    background-color: transparent;
  }
  79%{
    pointer-events: none;
    background-color: transparent;
  }
  80% {
    pointer-events: auto;
    background-color: yellow;
  }
  81% {
    pointer-events: none;
    background-color: transparent;
  }
  100%{
    pointer-events: none;
    background-color: transparent;
  }
}

我不确定,但是我认为您也可以为每个列表元素仅使用一个cicle并使用animation-delay。