简体   繁体   English

更改Div的背景图片和链接悬停时的内容?

[英]Change Div background image and content on link hover?

i've looked around a bit but most of the solutions are specific to the links background, and not the div's background. 我环顾了一下,但是大多数解决方案是特定于链接背景的,而不是div的背景。 I'm trying to understand how to image swap backgrounds for an entire div by hovering over a link in that specific div. 我试图通过将鼠标悬停在特定div上的链接上来了解如何为整个div交换背景图像。

By visiting this page: http://splash.org/ , and scrolling down to "Today we served" section, you can hover over the content there and the background image will change. 通过访问此页面: http : //splash.org/ ,并向下滚动到“今天我们已投放”部分,您可以将鼠标悬停在该页面上的内容上,背景图片将更改。

Thanks! 谢谢!

here is a little fiddle that can handle it : https://jsfiddle.net/pnok7euo/ 这是一个可以解决它的小提琴: https : //jsfiddle.net/pnok7euo/

And code for it. 并为其编写代码。

In a simple way : a div container is positionned relative. 以一种简单的方式:div容器是相对放置的。 Both 2 other divs are positionned in it in absolute (100% width/height). 其他两个div都以绝对值(100%宽度/高度)放置在其中。

A link is positionned, but it can be anywhere else. 链接已定位,但可以位于其他任何位置。

On JS, when the link is mouseover, we just fadeOut the first block and fadeIn the second one. 在JS上,当链接位于鼠标悬停时,我们只需淡出第一个块,淡入第二个块。

HTML : HTML:

<div id="container">
    <a href="" class="hover"></a>
    <div id="back">Back content ?</div>
    <div id="back2">Back 2 content ?</div>
</div>

JS : 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 : 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 ;
}

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

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