[英]Apply a CSS hover effect to an element that’s not a child of the hovered element
The tilde sign(~) only works on #header
. 波形符号(〜)仅适用于#header
。 But if I want to try it on li tag it's not working because it's inside of #header
. 但是如果我想在li标签上尝试它,它就不起作用了,因为它在#header
里面。
#header { background-color: red; } /* header:hover ~ .element { background-color:blue; } */ li:hover ~ .element { background-color: blue; } .element { background-color: green; }
<header id="header"> <li><a href="#">Hover</a> </li> </header> <div class="element"> <p>hello world</p> </div>
Do like this: 这样做:
#header { background-color: red; } /* header:hover ~ .element { background-color:blue; } */ #header:hover ~ .element { background-color: blue; } .element { background-color: green; }
<header id="header"> <li><a href="#">Hover</a> </li> </header> <div class="element"> <p>hello world</p> </div>
You have all your li elements inside the #header. 你有#header里面的所有li元素。 So, the div with class="element" will always be placed after the header. 因此,class =“element”的div将始终放在标题之后。 There is no need to select the li:hover 没有必要选择li:hover
It is not possible to complete with CSS only, assuming CSS has no methods to go up the elements tree. 假设CSS没有方法可以使用元素树,则无法仅使用CSS完成。 So you can't go back from child element to parent and then to parent's sibling. 所以你不能从子元素返回到父元素然后再转移到父母的兄弟元素。 It is possible to make with javascript/jQuery being involved. 可以使用javascript / jQuery。
Here is some quick and tiny example of how this can be using jQuery. 这是一个如何使用jQuery的快速而微小的例子 。
JS : JS :
$( '#nav a' ).on( 'mouseenter mouseleave', function() {
var that = $(this);
var target = '#' + that.data('target');
that.toggleClass('active');
$('#sectionTeaser').toggle().find(target).toggle();
});
HTML : HTML :
<div id="header">
<ul id="nav">
<li><a href="#" data-target="techSection">Tech</a></li>
<li><a href="#" data-target="financeSection">Finance</a></li>
<li><a href="#" data-target="politicsSection">Politics</a></li>
<li><a href="#" data-target="strategySection">Strategy</a></li>
</ul>
<div id="sectionTeaser">
<div id="techSection" class="s-teaser">Tech</div>
<div id="financeSection" class="s-teaser">Finance</div>
<div id="politicsSection" class="s-teaser">Politics</div>
<div id="strategySection" class="s-teaser">Strategy</div>
</div>
</div>
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim. Suspendisse id velit vitae ligula volutpat condimentum. Aliquam erat volutpat. Sed quis velit. Nulla facilisi. Nulla libero. Vivamus pharetra posuere sapien. Nam consectetuer. Sed aliquam, nunc eget euismod ullamcorper, lectus nunc ullamcorper orci, fermentum bibendum enim nibh eget ipsum. Donec porttitor ligula eu dolor. Maecenas vitae nulla consequat libero cursus venenatis. Nam magna enim, accumsan eu, blandit sed, blandit a, eros.</p>
<p>Quisque facilisis erat a dui. Nam malesuada ornare dolor. Cras gravida, diam sit amet rhoncus ornare, erat elit consectetuer erat, id egestas pede nibh eget odio. Proin tincidunt, velit vel porta elementum, magna diam molestie sapien, non aliquet massa pede eu diam.</p>
</div>
CSS (just for the sake of example, as you definitely need some other styling): CSS (仅为了示例,因为您肯定需要其他一些样式):
html, body {
margin:0;
padding:0;
font:12px/16px Arial, Helvetica, sans-serif;
}
#header {
position:relative;
height:40px;
}
#nav {
margin:0;
padding:0;
list-style:none;
font:12px/40px Arial, Helvetica, sans-serif;
}
#nav li {
float:left;
border:1px solid #666;
width:25%;
box-sizing:border-box;
text-align:center;
}
#nav a {
color:#333;
text-decoration:none;
display:block;
height:40px;
}
#nav a.active {
background:#999;
color:#333;
}
#sectionTeaser {
height:150px;
width:100%;
top:42px;
position:absolute;
display:none;
}
#sectionTeaser .s-teaser {
display:none;
position:absolute;
bottom:0;
left:0;
top:0;
right:0;
height:150px;
font:18px/150px Arial, Helvetica, sans-serif;
background:#fff;
text-align:center;
text-transform:uppercese;
}
So, the idea is to use list of links and separated blocks with content which are connected by data
-attribute. 因此,我们的想法是使用链接列表和带有内容的分隔块,这些内容通过data
-attribute连接。 Once we hover a link, js (jQuery) finds the corresponding block and reveals it. 一旦我们悬停链接,js(jQuery)会找到相应的块并显示它。
With this code it works as expected: 使用此代码,它按预期工作:
#header { background-color: red; } #header:hover ~ .element { background-color: blue; } .element { background-color: green; }
<header id="header"> <li><a href="#">Hover</a> </li> </header> <div class="element"> <p>hello world</p> </div>
You'll have to use javascript if you dont want to change your HTML structure. 如果您不想更改HTML结构,则必须使用javascript。 http://jsfiddle.net/mwno6869/7/ http://jsfiddle.net/mwno6869/7/
$("#header li").mouseenter(function() {
$('.element').css("background", "blue");
}).mouseleave(function() {
$('.element').css("background", "green");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.