简体   繁体   English

如何悬停div并使其背景图像慢慢消失而又不影响其内部内容?

[英]How to hover a div and make its background-image slowly dissapear without affecting the content that's inside it?

I have a div with a background-color , background-image and some text on it . 我有一个背景颜色,背景图像和它的一些文字股利。 I want , when i hover the div , the background-image to slowly dissapear, like a transition effect or something, but the text and the color still remain there visible in the div.I am open to any type of method. 我想当我将鼠标悬停在div上时,背景图像像过渡效果之类的东西慢慢消失,但是文本和颜色仍然在div中仍然可见。我可以使用任何类型的方法。

这是div悬停前的外观

这就是悬停时应该的样子

You can do this easily using only CSS adding a few DOM elements. 您只需使用CSS添加一些DOM元素即可轻松完成此操作。

 .fadebg { position: relative; height: 100px; width: 100px; background-color: red; } .fadebg:hover .img, .fadebg:hover img { opacity: 0; } .text { position: absolute; top: 0; left: 0; right: 0; bottom: 0; color: white; z-index: 1; } .img { background-repeat: no-repeat; background-image: url(https://upload.wikimedia.org/wikipedia/en/b/bc/Screenshot_Snarf.jpg); background-size: 100px 100px; } .img, img { opacity: 1; height: 100px; width: 100px; position: absolute; top: 0; left: 0; bottom: 0; right: 0; overflow: hidden; -webkit-transition: 1s all; -moz-transition: 1s all; -ms-transition: 1s all; -o-transition: 1s all; transition: 1s all; } 
 <div class="fadebg"> <div class="text">Hi</div> <div class="img"></div> </div> <br> <div class="fadebg"> <div class="text">Hi</div> <img src="https://upload.wikimedia.org/wikipedia/en/b/bc/Screenshot_Snarf.jpg"> </div> 

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

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