css 简单悬停淡入淡出

标签 css image transition fade

如果有人能告诉我我要去哪里错了,我只是在徘徊,试图让一个图像褪色以在悬停时显示其下方的另一个图像。但是,我似乎无法让两个图像重叠。任何帮助将不胜感激!

HTML

<div id="content" class="sixteen columns textcenter box">
<p>
       <img class="top" src="img/homepageimg2.jpg" alt="Photography Thumbnail" align="center"> 
       <img class="bottom" src="img/homepageimg22.jpg" alt="Photography Thumbnail" align="center">
</p>
</div>

CSS

#content {
z-index:10;
position:relative;
background-image:url(../img/backgroundindex.jpg);
background-repeat: repeat;  
padding-top: 50px;
padding-bottom:50px;

}
#content img{
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}

#content img.top hover {
opacity:0;
}

谢谢!

最佳答案

悬停是一种伪造,因此如果您希望在类为“top”的 img(#content 的子级)悬停时应用效果,您需要使用以下形式:

#content img.top:hover {
  opacity: 0;
}

尝试在 .top 和 .bottom 上使用绝对定位(加上 top:0、bottom:0 等),并在它们的父容器上使用相对定位。确保 .top 的 z-index 高于 .bottom。

我已经在 CodePen 上设置了这个效果的完整演示:http://cdpn.io/aAjBb

演示 HTML:

<div class="hover-swap">
  <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-256px-SIPI_Jelly_Beans_4.1.07.tiff.jpg" class="top"/>
  <img src="http://myhswm.org/images/sized/images/animals/tuxedo_kittens-256x256.jpg" class="bottom"/>
</div>

演示 CSS:

.hover-swap {
  position: relative;
  height: 256px;
  width: 256px;
}

.hover-swap .top {
  opacity: 1;
  z-index: 2;
}

.hover-swap .bottom {
  opacity: 1;
  z-index: 1;
}

.hover-swap:hover .top {
  opacity: 0;
}

.hover-swap .bottom, .hover-swap .top {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

关于css 简单悬停淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011150/

相关文章:

html - 在wordpress中将按钮放置在全宽和高度图像上

ios - 在呈现另一个 View Controller 时,如何阻止 "from" View 变黑?

html - 更改/修复导航栏上的字体

html - Bootstrap 4 - col 垂直对齐不按预期工作

css - 使用带有 CssResource 和 @if 的 GWT 的移动设备的不同字体大小

html - 在页脚中排列图片

python - 通过python 3将表从图像提取到另一个图像

c++ - 在 OpenCV 中合并两个 Mat 对象

javascript - 为悬停时显示隐藏跨度的按钮创建平滑过渡

ios - 在 Swift 中加载 UIView 时自定义转换出错