html - 如何创建淡入/淡出图像动画

标签 html css css-animations

我想调整下面正在运行的现有代码,以支持不止两个图像。

HTML:

<div id="cf">
  <img class="bottom" src="<?php bloginfo('template_directory') ?>/assets/img/image1" />
  <img class="top" src="<?php bloginfo('template_directory') ?>/assets/img/image2" />
</div>

CSS:

#cf {
  position:relative;
}

#cf img {
  position:absolute;
  left:0;
  -webkit-transition: opacity 4s ease-in-out;
  -moz-transition: opacity 4s ease-in-out;
  -o-transition: opacity 4s ease-in-out;
  transition: opacity 4s ease-in-out;
}

@keyframes cf3FadeInOut {
  0% {
    opacity:1;
  }
  45% {
    opacity:1;
  }
  55% {
    opacity:0;
  }
  100% {
    opacity:0;
  }
}

#cf img.top {
  animation-name: cf3FadeInOut;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 4s;
  animation-direction: alternate;
}

我知道它很接近,但我不确定如何编写它以支持更多图像。想法或建议?

最佳答案

设置一个包含动画的类。 此类用于所需的图像。

例子

@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.fadeOut {
  opacity: 0;
  animation-name: fadeOut;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 4s;
  animation-direction: alternate;
}
<img class="fadeOut" src="http://via.placeholder.com/40x40">
<img class="fadeOut" src="http://via.placeholder.com/40x40">
<img class="fadeOut" src="http://via.placeholder.com/40x40">

关于html - 如何创建淡入/淡出图像动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50590939/

相关文章:

javascript - 滚动到位置的链接

html - 如何将按钮移动到第一个文本列的中间?

javascript - 我正在尝试使用 javascript 单击按钮时显示表单

jquery - 清除 HTML 中的单元格值

html - 在非本地服务器上将 reveal.js 幻灯片 (.html) [Jupyter 笔记本导出] 作为网页托管

jquery - 将内容移出浏览器区域

css - 在 Bootstrap 容器中移动内容的正确方法

html - 是否可以通过仅使用 html 和 css 按下按钮来获得动画?

css - 如何解决此 Codepen 上的这两个问题?

html - 如何正确渲染和动画 svg 图标?