【问题标题】::hover style on div with an image that doesnt trigger:hover 样式在 div 上,带有不触发的图像
【发布时间】:2016-09-21 07:38:09
【问题描述】:

我一直在尝试用纯 css 在网站上制作覆盖悬停效果

                       <div class="col-md-4 port-show">
                            <img class="img-responsive" alt="" src="">
                            <h2 class=""></h2>
                            <ul>
                                <li></li>
                                <li></li>
                            </ul>
                        </div>

但是我遇到了问题当我将鼠标悬停在 img 上时,覆盖不会触发。我知道我必须以不同的方式做,但现在不要这样做。 CSS:

:hover:after{
        content: "";
        z-index: 10;
        display: block;
        position: absolute;
        height: 100%;
        top: 0;
        left: 0;
        right: 0;
        background: red;
        border-radius: 6px;
        border-color: transparent;
    }

【问题讨论】:

  • 您将鼠标悬停在哪个元素上?
  • 不要认为你可以在图像上做一个 after 因为它将元素放置在元素的末尾(内部)并且 img 标签不能有子元素,同样你正在定位绝对之后,您必须使 img 相对
  • @JayGhosh 我试图悬停 div,但是当我有点太快并将鼠标悬停在图像上时,我的悬停效果不起作用。
  • I'm a little too quick and hover over the image。这取决于您的浏览器捕获悬停事件的能力 bro

标签: html css hover overlay pure-css


【解决方案1】:

你可以看这里。也许这个解决方案会对你有所帮助。

http://codepen.io/anon/pen/VKmxZw

.img-holder {
  position: relative;
  z-index: 1;
  display: block;
  width: 350px;
}
.img-holder img {
  display: block;
  width: 100%;
}
.img-holder:after {
  content: " ";
  width: 100%;
  height: 100%;
  background-color: red;
  z-index: 2;
  left: 0;
  top: 0;
  position: absolute;
  opacity: 0;
}
.img-holder:hover:after {
  opacity: 1;
}
<div class="col-md-4 port-show">
  <span class="img-holder">
    <img src="http://placehold.it/350x150" />
  </span>
  <h2 class=""></h2>
  <ul>
    <li></li>
    <li></li>
  </ul>
</div>

【讨论】:

  • 谢谢!皮特,我相信这可能解决了我的难题:D
【解决方案2】:

你可以按照这个来制作你的叠加效果

@import url(http://fonts.googleapis.com/css?family=Open+Sans);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Open Sans", sans-serif;
}

a {
  color: inherit;
  text-decoration: none;
}

h2 {
  color: #c55;
  padding: 50px 0;
  text-align: center;
  text-transform: uppercase;
}

.container {
  background: url("http://static.pexels.com/wp-content/uploads/2015/03/fog-forest-haze-4827-524x350.jpeg");
  background-size: cover;
  background-position: center;
  position: relative;
  margin: auto;
  width: 200px;
  height: 200px;
  cursor: pointer;
  overflow: hidden;
}
.container:hover .overlay {
  opacity: 1;
  width: 100%;
  height: 100%;
}
.container:hover .overlay span {
  opacity: 1;
  -webkit-transition: 1.3s ease;
  transition: 1.3s ease;
}
.container .overlay {
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  margin: auto;
  width: 0px;
  height: 0px;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  opacity: 0;
  -webkit-transition: .3s ease;
  transition: .3s ease;
}
.container .overlay span {
  color: #fff;
  text-align: center;
  position: absolute;
  margin: auto;
  width: 180px;
  height: 30px;
  line-height: 30px;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
}
<h2>Image Overlay Hover Effect</h2>
<div class="container">
  <a href="#">
    <div class="overlay">
      <span>Click to see more...</span>
    </div>
  </a>
</div>

【讨论】:

    【解决方案3】:

    一些简单的覆盖代码:

    <!DOCTYPE html>
    <html >
    <head> 
    <style type="text/css"> 
      .pic{ width:190px;
       height:190px; opacity: 1; 
       filter: alpha(opacity=100); 
       background: url(http://www.corelangs.com/css/box/img/duck.png) no-repeat; }
       .pic:hover { opacity: 0.3; filter: alpha(opacity=30);}
    </style> 
    </head> 
    <body> 
    <div class="pic">
    </div> 
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多