简体   繁体   English

将鼠标悬停在CSS图像上并淡入

[英]CSS Image overlay on hover with fade in

I've been trying to create the following: Showing an image and when hovering over it, on the image a score will show, which fades in. I'm almost there, except for the fading in part. 我一直在尝试创建以下内容:显示图像,并将其悬停在图像上时,分数会逐渐消失。除部分褪色外,我几乎就在那儿。

My CSS: 我的CSS:

 .profile-image10 { opacity: 1; } .profile-image10:hover .overlay { position: absolute; width: 100%; height: 100%; left: 510px; top: 8px; z-index: 2; background: transparent url('http://www.defilmkijker.com/wp-content/uploads/2018/01/overlayscore10.png') no-repeat; -webkit-transition: opacity 1s ease-in; -moz-transition: opacity 1s ease-in; -ms-transition: opacity 1s ease-in; -o-transition: opacity 1s ease-in; transition: opacity 1s ease-in; opacity: 0.1; } 
 <div class="profile-image10"> <a href="#"><img src="https://www.w3schools.com/howto/img_fjords.jpg" /></a> <span class="overlay"></span> </div> 

I've created this fiddle: https://jsfiddle.net/1wusom4m/ 我创建了这个小提琴: https : //jsfiddle.net/1wusom4m/

As you can see it currently fades out when hovering, which is the opposite of what I want. 如您所见,它当前在悬停时逐渐消失,这与我想要的相反。 So of course the opacity is set the wrong way around, but if I set it to a low value in the .profile-image10 it affects the original image as well. 因此,当然不透明性的设置方法错误,但是如果我在.profile-image10中将其设置为较低的值,它也会影响原始图像。 I'm quite new to this, so have been experimenting a lot to get this far, but I'm stuck now. 我对此还很陌生,因此一直在做很多尝试以达到目标,但现在我陷于困境。 So how can I make it fade in the score when hovering over the image? 因此,当将鼠标悬停在图像上时,如何使它在分数中消失? Or am I approaching this all wrong? 还是我把这一切都弄错了?

Three things. 三件事。

  1. Don't depend all style properties for your overlay on :hover state. 不要将所有样式属性都依赖于:hover状态。 Only those you want to change on :hover of the image. 仅您要在图像的:hover上更改的内容。

  2. Always define the transition property for the original state. 始终为原始状态定义transition属性。 You can change the reverse transition by additionally specifying it on the :hover state. 您可以通过在:hover状态上另外指定反向转换来更改反向转换。

  3. You div is a block level element, which by default grabs as much horizontal space as available. div是一个块级元素,默认情况下它会获取尽可能多的水平空间。 Putting the :hover on the div results in the fade effect being triggered even if you hover right of the picture (which probably is unwanted). :hover放在div导致淡入淡出效果,即使您将鼠标悬停在图片的右边(这可能也是不需要的)。

 .profile-image10 .overlay{ opacity: 0; position: absolute; width: 100%; height: 100%; left: 585px; top: 8px; z-index: 2; background: transparent url('http://www.defilmkijker.com/wp-content/uploads/2018/01/overlayscore10.png') no-repeat; -webkit-transition: opacity 1s ease-in; -moz-transition: opacity 1s ease-in; -ms-transition: opacity 1s ease-in; -o-transition: opacity 1s ease-in; transition: opacity 1s ease-in; } .profile-image10 a:hover + .overlay { opacity: 1; } 
 <div class="profile-image10"> <a href="#"><img src="http://www.defilmkijker.com/wp-content/uploads/2018/01/Recensie-Three-Billboards-Outside-Ebbing-Missouri.jpg" class="alignnone size-full wp-image-19857 round" /></a> <span class="overlay"></span> </div> 

Fiddle: https://jsfiddle.net/1wusom4m/3/ 小提琴: https : //jsfiddle.net/1wusom4m/3/

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

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