我看了一下,但大多数解决方案都是特定于链接背景,而不是div的背景。我试图通过将鼠标悬停在特定div中的链接上来了解如何为整个div设置交换背景。
访问此页面:http://splash.org/,然后向下滚动到“今天我们投放”部分,您可以将鼠标悬停在那里的内容上,背景图片也会发生变化。
谢谢!
答案 0 :(得分:2)
这是一个可以处理它的小提琴:https://jsfiddle.net/pnok7euo/
并为其编码。
以一种简单的方式: div容器相对定位。其他2个div都以绝对值(100%宽度/高度)定位在其中。
链接已定位,但它可以位于其他任何位置。
在JS上,当链接是鼠标悬停时,我们只是淡出第一个块并淡出第二个块。
HTML:
<div id="container">
<a href="" class="hover"></a>
<div id="back">Back content ?</div>
<div id="back2">Back 2 content ?</div>
</div>
JS:
jQuery(document).on('mouseover','a.hover',function(){
jQuery('div#back').stop().fadeOut() ;
jQuery('div#back2').stop().fadeIn() ;
}) ;
jQuery(document).on('mouseout','a.hover',function(){
jQuery('div#back2').stop().fadeOut() ;
jQuery('div#back').stop().fadeIn() ;
}) ;
CSS:
div#container {
width:500px ;
height:300px ;
position:relative ;
}
div#back, div#back2 {
position:absolute ;
top:0 ; left:0 ; right:0 ; bottom:0 ;
background-position:center center ;
background-repeat:no-repeat ;
color:white ;
text-align:center ;
font-size:2em ;
}
div#back {
background-image:url('http://angeoudemongif.a.n.pic.centerblog.net/d3e42620.gif') ;
z-index:4 ;
}
div#back2 {
background-image:url('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg') ;
z-index:2 ;
}
a.hover {
position:absolute ;
display:block ;
background:white ;
opacity:0.8 ;
padding:10px ;
top:10px ;
left:10px ;
z-index:10 ;
}