简体   繁体   English

悬停时叠加透明图像

[英]Overlay transparent image on hover

I have a <div> with a background-image . 我有一个带有background-image<div> When this is hovered over I would like another image to be placed on top partially transparent so the original image can be seen below. 当将鼠标悬停在上面时,我希望将另一幅图像放置在部分透明的顶部,以便可以在下面看到原始图像。

My current idea involved adding a :hover state and changing the above images display state to visible along with necessary z-index values. 我当前的想法涉及添加:hover状态并将上述图像display状态与必要的z-index值一起更改为visible

Could someone give me an example with jsfiddle.net implementation? 有人可以给我一个jsfiddle.net实现的例子吗?

Why not use opacity ? 为什么不使用不透明度

The opacity CSS property specifies the transparency of an element, that is, the degree to which the background behind the element is overlaid. opacity CSS属性指定元素的透明度,即元素后面的背景覆盖的程度。

The value applies to the element as a whole, including its contents, even though the value is not inherited by child elements. 该值适用于整个元素,包括元素的内容,即使该值未被子元素继承。 Thus, an element and its contained children all have the same opacity relative to the element's background, even if the element and its children have different opacities relative to one another. 因此,一个元素及其包含的子元素相对于元素的背景都具有相同的不透明度,即使该元素及其子元素相对于彼此具有不同的不透明度。

.myTransparentImage{
   opacity: 0;
}

.myTransparentImage:hover{
   opacity: 0.6; /* it's in pourcentage */
}

This way, the transparent image, on hover, will appear at 60% opacity so you can still see the one below. 这样,悬停时透明图像将以60%的opacity出现,因此您仍然可以看到下面的图像。 So it is on top of the other image the whole time but only appears once hovered. 因此,它始终位于其他图像的顶部,但仅在悬停一次之后才会显示。

Here is an example in a fiddle: https://jsfiddle.net/5ob6n7nq/ 这是一个小提琴的例子: https : //jsfiddle.net/5ob6n7nq/

Whipped up a quick example for you. 为您准备了一个简单的示例。 Hit "Run code snippet" to see it in action. 点击“运行代码段”以查看其运行情况。

 .image-holder { background: url('http://i.imgur.com/5ln9Vmi.jpg'); height: 500px; width: 500px; position: relative; } .image-holder::before { content: ''; background: url('http://i.imgur.com/khYHDfJ.jpg'); height: 100%; width: 100%; position: absolute; top: 0; left: 0; opacity: 0; transition: opacity .5s; } .image-holder:hover::before { opacity: .5; /* amount of opacity to blend the two images */ } 
 <div class="image-holder"> </div> 

If I correctly understand you: https://jsfiddle.net/3jabz7d3/ 如果我正确理解你的话: https : //jsfiddle.net/3jabz7d3/

<div class="block1">
  <div class="block2"></div>
</div>


.block1 {
  position: relative;
  width: 200px;
  height: 200px;
  background: url(http://writm.com/wp-content/uploads/2016/08/Cat-hd-wallpapers-1080x675.jpg)  no-repeat center center;
  background-size: cover;
}

.block2 {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url(http://www.cats.org.uk/uploads/images/pages/photo_latest14.jpg)  no-repeat center center;
  background-size: cover;
  display: none;
  opacity: 0.3;
}

.block1:hover .block2{
  display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM