我怎样才能实现这种悬停效果?

时间:2016-07-27 00:06:46

标签: html css

我目前正在反向构建这个网站,其他一切正常,但是,我找不到像这样实现照片悬停效果的方法。

http://www.lovelytemplates.com/viewdemo/Snaps

这是我的代码,任何人都可以告诉我,我做错了什么?

img {
  width: 236px;
  height: 314px;
}
div:hover {
  opacity: 0.9;
  color: #fff;
  background: rgba(0, 0, 0, 0.9);
}
<div>
  <img src="http://demo.graphpaperpress.com/snaps/files/2013/04/child-72305-480x640.jpg" alt="" />asdfasdf asd
</div>

3 个答案:

答案 0 :(得分:1)

试一试?我没有看原始网站是如何做到的。很可能有更好的方法:

img {
  width: 236px;
  height: 314px;
  position: absolute;
}

div.overlay {
  width: 236px;
  height: 314px;
  position: absolute;
  background-color: black;
  color: white;
  display: none;
  line-height: 314px;
  font-family: Arial;
  text-align: center;
}

div.container:hover div.overlay {
  width: 236px;
  height: 314px;
  background-color: rgba(0, 0, 0, 0.8);
  display: block;
}
<div class="container">
  <img src="http://demo.graphpaperpress.com/snaps/files/2013/04/child-72305-480x640.jpg" alt="">
  <div class="overlay">
    Text goes here.
  </div>
</div>

答案 1 :(得分:0)

你可以这样轻松地做到这一点。我使用了以下组合:

  • opacity
  • translateY
  • position

div {
  position: relative;
  display: inline-block;
}

img {
  width: 236px;
  height: 314px;
  opacity: .8
}
div:hover img,
div:hover .caption {
  opacity: 1;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
}

.caption {
  position: absolute;
  z-index: 999;
  top: 0; bottom: 0;
  right: 0; left: 0;
  background: rgba(0,0,0,0.5);
  opacity: 0;
}

.caption span {
  top: 50%;
  right: 0; left: 0;
  transform: translateY(-50%);
  color: #fff;
  text-align: center;
  display: block;
  position: absolute;
}
<div>
  <img src="http://demo.graphpaperpress.com/snaps/files/2013/04/child-72305-480x640.jpg" alt="" />
  <div class="caption"><span>Hello World</span></div>
</div>

答案 2 :(得分:0)

试试这个对悬停

的完全相同的效果
.title {
    font-size: 8em;
    font-weight: bold;
    text-shadow: 7px 7px 0 rgba(0, 0, 0, 0.5);
    text-transform: uppercase;
    font-family: sans-serif;
    color: #fff;
    opacity: 0.8;
}

    .title :hover {
        opacity: 1;
    }