html - 如何仅从CSS更改图像?

标签 html css

我有这个样本:

link

代码HTML:

<div id="cf2" class="shadow">
  <img class="bottom" src="http://assets.sport.ro/assets/protv/2014/12/31/image_galleries/40908/cele-mai-frumoase-peisaje-din-lume-locurile-pe-care-trebuie-sa-le-vizitezi_1_size19.jpg" />
  <img class="top" src="http://assets.sport.ro/assets/protv/2014/12/31/image_galleries/40908/cele-mai-frumoase-peisaje-din-lume-locurile-pe-care-trebuie-sa-le-vizitezi.jpg" />
</div>


代码CSS:

img{
  width:300px;
}
@keyframes cf3FadeInOut {
  0% {
    opacity:1;
  }
  45% {
    opacity:1;
  }
  55% {
    opacity:0;
  }
  100% {
    opacity:0;
  }
}

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


我想使用关键帧在它们之间交换图像。

我试图重现示例here之一,但某些方法不起作用。

你能告诉我我做错了吗?
先感谢您!

最佳答案

您正在访问CSS中错误的ID



img{
  width:300px;
  /* added this position to make images overlap */
  position: absolute; 
}
@keyframes cf3FadeInOut {
  0% {
    opacity:1;
  }
  45% {
    opacity:1;
  }
  55% {
    opacity:0;
  }
  100% {
    opacity:0;
  }
}

/* here your name of id was wrong */
#cf2 img.top {
    animation-name: cf3FadeInOut;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-duration: 2s;
    animation-direction: alternate;
}

<div id="cf2" class="shadow">
  <img class="bottom" src="http://assets.sport.ro/assets/protv/2014/12/31/image_galleries/40908/cele-mai-frumoase-peisaje-din-lume-locurile-pe-care-trebuie-sa-le-vizitezi_1_size19.jpg" />
  <img class="top" src="http://assets.sport.ro/assets/protv/2014/12/31/image_galleries/40908/cele-mai-frumoase-peisaje-din-lume-locurile-pe-care-trebuie-sa-le-vizitezi.jpg" />
</div>

关于html - 如何仅从CSS更改图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45960234/

相关文章:

jquery - -webkit-column-count从上到下而不是从左到右读取div

css - 为什么 "initial"关键字指定的 CSS 样式与默认样式不同?

javascript - Blogspot 的 HTML/CSS/JS(可能)问题

javascript - 如何在 Ace Editor 中显示文件内容

javascript - 检测文本框是否不包含某些单词

jquery - 仅在跨度内包装文本

html - 学习 HTML 和学习编程语言有什么区别?

asp.net - UpdatePanel 输入类型不是 html5 中的文本

javascript - 将 JScript 添加到 prestashop CMS 页面

javascript - Matrix3d属性值及其他