css - 纯 CSS 悬停停止动画

标签 css css-animations keyframe onhover

我需要在悬停时停止动画 Codepen元素。

我认为添加

.photo:hover{
animation-play-state:paused;
-webkit-animation-play-state:paused;
}

可能就足够了,但它仅适用于可见图像,而其他三个动画则在后台继续。

如何解决?

感谢您的阅读

最佳答案

您应该在 .container 上使用 :hover 伪类,然后在所有 .photo 上暂停动画

body {
  background: #000;
}

.container {
  margin: 50px auto;
  width: 500px;
  height: 300px;
  overflow: hidden;
  border: 10px solid;
  border-top-color: #856036;
  border-left-color: #5d4426;
  border-bottom-color: #856036;
  border-right-color: #5d4426;
  position: relative;
}

.photo {
  position: absolute;
  animation: round 16s infinite;
  opacity: 0;
}

@keyframes round {
  25% {
    opacity: 1;
  }
  40% {
    opacity: 0;
  }
}
img:nth-child(1) {
  animation-delay: 12s;
}

img:nth-child(2) {
  animation-delay: 8s;
}

img:nth-child(3) {
  animation-delay: 4s;
}

img:nth-child(4) {
  animation-delay: 0s;
}
.container:hover .photo {
animation-play-state:paused;
-webkit-animation-play-state:paused;
}
<div class="container">
  <img class='photo'  src="http://farm9.staticflickr.com/8320/8035372009_7075c719d9.jpg" alt="" />
  <img class='photo'  src="http://farm9.staticflickr.com/8517/8562729616_35b1384aa1.jpg" alt="" />
  <img class='photo'  src="http://farm9.staticflickr.com/8465/8113424031_72048dd887.jpg" alt="" />
  <img class='photo'  src="http://farm9.staticflickr.com/8241/8562523343_9bb49b7b7b.jpg" alt="" />

</div>

关于css - 纯 CSS 悬停停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43825308/

相关文章:

html - 使用属性缩放不好吗?

html - 更改 HTML 中的宽度时,文本对齐会发生变化

css - 在 CSS 动画结束时保持最终状态

html - 如何在 ionic 4 页面加载上实现 Celebration Confetti 动画?

ffmpeg - 检查关键帧间隔?

javascript - 为什么我必须在 <a> 上单击两次才能让 jQuery 更改它的 CSS?

javascript - 禁用输入文本访问但保持元素启用

css - 使用 CSS3 动画箭头以提供循环移动效果

javascript - 使用 border 和 border-radius 构建的动画心电脉冲线

html - CSS 在悬停时开始和暂停动画