简体   繁体   English

将CSS悬停效果应用于不是悬停元素的子元素的元素

[英]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.

相关问题 您可以将CSS悬停效果应用于不是悬停元素的子元素的元素吗? - Can you apply a CSS hover effect to an element that’s not a child of the hovered element? 将悬停效果应用于子元素 - Apply hover effect on child element 仅当一个元素的悬停元素是另一个元素的子元素时,才将样式应用于该元素的悬停吗? - Apply style on hover of an element to another element only if this other element is a child of the hovered element? CSS 网格 hover 对子元素的影响不起作用 - CSS Grid hover effect on Child Element not working 在 css 悬停效果上,无法选择/应用效果到特定元素? - On a css hover effect, not able to select/apply effect to a specific element? 当一个元素悬停并影响css中的另一个元素/类时,如何添加悬停效果? - How to add hover effect when one element hovered and affect on another element / class in css? CSS 效果 :hover::after 在子元素上 :hover 在父元素上 - CSS effect :hover::after on child element on :hover on parent 如何使这种悬停效果(悬停1按钮更改元素的ID,然后在悬停时将其更改回其原始ID?)[jquery,css3] - How do I make this hover effect (hover 1 button to change an element's ID then when hovered-out it changes back to it's original ID?) [jquery,css3] 如何让伪元素悬停时不出现hover效果 - How to make hover effect not appear when pseudo-element is hovered on 当元素不再悬停时,停止jQuery悬停效果 - Stop jQuery hover effect when element is not hovered anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM