根据堆栈溢出中发现的to a trick,我可以改变像这样悬停孩子的父元素的背景:
parent sibling { }
parent sibling:hover { }
parent:hover sibling { }
但是我需要改变悬停在不同子元素上的背景颜色。例如,将Facebook按钮悬停,背景变为蓝色,同时将鼠标悬停在Google +按钮上,背景变为红色。
我试过这个:
<div class="share_box">
<div class="white-container">
<div class="facebook-sibling"/>
<a class="facebook-child-button"/>
<div class="twitter-sibling"/>
<a class="twitter-child-button"/>
<div class="googleplus-sibling"/>
<a class="googleplus-child-button"/>
</div>
</div>
答案 0 :(得分:2)
这是你想要的吗?
DEMO 1: http://jsfiddle.net/t73431y8/
DEMO 2: http://jsfiddle.net/t73431y8/2/
<强> HTML:强>
<div class="PARENT">
<div class="RED">RED</div>
<div class="BLUE">BLUE</div>
<div class="GREEN">GREEN</div>
</div>
CSS:
.PARENT{
position: relative;
}
.RED{
display: inline-block;
margin: 5px;
padding: 5px;
color: #BB0000;
background: #FFF;
}
.RED:hover:after{
display: block;
width: 100%;
height: 100%;
background: #BB0000;
position: absolute;
top: 0px;
left: 0px;
content: ' ';
z-index: -1;
}
.BLUE{
display: inline-block;
margin: 5px;
padding: 5px;
color: #0000BB;
background: #FFF;
}
.BLUE:hover:after{
display: block;
width: 100%;
height: 100%;
background: #0000BB;
position: absolute;
top: 0px;
left: 0px;
content: ' ';
z-index: -1;
}
.GREEN{
display: inline-block;
margin: 5px;
padding: 5px;
color: #00BB00;
background: #FFF;
}
.GREEN:hover:after{
display: block;
width: 100%;
height: 100%;
background: #00BB00;
position: absolute;
top: 0px;
left: 0px;
content: ' ';
z-index: -1;
}
答案 1 :(得分:2)
如果您设置了父position: relative
,则它将包含任何position: absolute
个孩子。
在父元素的末尾创建一个新元素,然后将其设为position: absolute
并对其进行定位和调整,使其填充父元素。
然后使用z-index: -1
将其设置在其余内容后面(例如按钮)。
然后您可以使用General Sibling Combinator (~
)在悬停元素之后选择新元素。
.facebook:hover ~ .background { background-color: rgb(50, 100, 150); }
.twitter:hover ~ .background { background-color: rgb(50, 150, 250); }
.google:hover ~ .background { background-color: rgb(250, 75, 50); }
.share {
position: relative;
}
.background {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: -1;
} /* Following styling for demo purposes only, not relevant */ .facebook:before { background-position:-46px -28px; width:101px; } .twitter:before { background-position:-151px -28px; width:90px; } .google:before { background-position:-245px -28px; width:94px; } .button:before { display:inline-block; content: ""; height:36px; background-image:url("http://i.stack.imgur.com/AXvMk.png"); border-radius: 3px; box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.2); } .button { display:inline-block; padding: 2px; } .white-container { padding: 10px 20px; font-size: 0; background: #fff; border-radius: 3px; } .background { background: #fff; } body { margin: 0 4px; border: 1px solid #aaa; border-top: 0px; box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.1) } .share { padding: 10px 15px; box-shadow: 0px 5px 5px -5px rgba(0,0,0,0.3) inset } body:before { content: ''; height: 4px; display: block; background: #fff; border-bottom: 1px solid #aaa } html { background: #efefef }
<div class="share">
<div class="white-container">
<a href="#" class="button facebook"></a>
<a href="#" class="button twitter"></a>
<a href="#" class="button google"></a>
<div class="background"></div>
</div>
</div>