将鼠标悬停在叠加层上后,如何将图像保留在图像上?

时间:2016-09-28 17:32:58

标签: javascript hover

我的图片上有文字,上面有文字 当我将鼠标悬停在它上面时,叠加层会消失,但文字也会消失。 我希望文本保留在图像的顶部,如何编辑我的代码呢? 这是我的css代码:

div.homepage-popular-categories {
    position: relative;
    display: inline-block;
}

div.homepage-popular-categories p {
  margin: 0;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  color: #eeeeec;
  background: rgba(0,0,0,0.5);
  transition: opacity 0.5s;
  opacity: 1;
  text-align: center;
  font-family: sans-serif;
  font-size: 1.2em;
  font-weight: bold;
}

div.homepage-popular-categories p:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
div.homepage-popular-categories p:hover {
  opacity: 0;
  transition: opacity 0.5s;
  cursor: default;
}

这是我的小提琴:
https://jsfiddle.net/7xax9p5e/

1 个答案:

答案 0 :(得分:3)

不使用opacity转换,而是使用background转换。



div.homepage-popular-categories {
  position: relative;
  display: inline-block;
}

div.homepage-popular-categories p {
  margin: 0;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  color: #eeeeec;
  background: rgba(0,0,0,0.5);
  transition: background 0.5s;
  opacity: 1;
  text-align: center;
  font-family: sans-serif;
  font-size: 1.2em;
  font-weight: bold;
}

div.homepage-popular-categories p:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
div.homepage-popular-categories p:hover {
  background: rgba(0,0,0,0);
  cursor: default;
}

<div class="homepage-popular-categories">
	<img src="http://www.eonline.com/eol_images/Entire_Site/20131022/rs_560x415-131122130228-1024.family-guy.jpg">
	<p>Family Guy</p>
	
</div>
&#13;
&#13;
&#13;