简体   繁体   English

悬停此元素时隐藏伪后元素

[英]Hide pseudo after element when hovering this element

My menu is placed vertically on the left side of the page and between the <li> I have an :after that is a separator. 我的菜单垂直放置在页面的左侧,在<li>之间我有一个:after是分隔符。 What I want to have is to hide the after element when I hover the element itself (if it's the first element) or the above and the bottom one when it's the middle one and if it's the last child just the :after element of the previous <li> . 我想有是隐藏的元素后,当我将鼠标悬停在元素本身(如果它是第一个元素)或以上和底部的一个时,它的中间的一个,如果它是最后一个孩子刚:after以前的元素<li> This may sound confusing but here is my code: 这可能听起来令人困惑,但这是我的代码:

 .menu { float: left; color: white; } .menu > ul { list-style: none; text-align: center; padding: 0; margin: 0; } .menu > ul > li { display: block; position: relative; padding: 60% 5px; background-color: #048990; cursor: pointer; transition: all 0.5s; } .menu > ul > li:hover { background-color: #444; color: white; } .menu > ul > li:hover .menu > ul > li::after { opacity: 0; } .menu > ul > li:active { background-color: #444; } .menu >ul >li:after { content: ""; background: #FFF; position: absolute; bottom: 0; left: 7%; height: 1px; width: 86%; } .menu > ul > li:last-child:after { display: none; } 
 <div class="menu"> <ul> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> </ul> </div> 

JSFiddle 的jsfiddle

Now all I want to do is something like this: 现在我想做的就是这样:

.menu > ul > li:hover + .menu > ul > li::after
{
    opacity: 0;
}

But it's not working. 但它不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

The below selector would not work because the full selector should not be repeated when using + adjacent sibling combinator. 以下选择器不起作用,因为使用+相邻同级组合器时不应重复完整选择器。 This would try to select the li::after that is under .menu > ul which is in-turn the adjacent sibling of the li that is being hovered on. 这将尝试选择li::after .menu > ul ,这将是正在盘旋的li的相邻兄弟。

.menu > ul > li:hover + .menu > ul > li::after {
    opacity: 0;
}

It should instead be written as .menu > ul > li:hover::after if you want to select the ::after of the li that is being hovered on (or) as .menu > ul > li:hover + li::after if you want to select the ::after element of the li that is the adjacent sibling of the li which is being hovered on. 应该不是写成.menu > ul > li:hover::after如果您想选择::after的的li上正在徘徊(或)作为.menu > ul > li:hover + li::after ,如果你想选择::after的元素li那是相邻兄弟li其正在徘徊的。


The other problem with the approach that you are using currently is that CSS selectors can be used only to select the children, descendants or the siblings that appear after the current element in DOM. 您当前使用的方法的另一个问题是CSS选择器只能用于选择DOM中当前元素之后出现的子节点,子节点或兄弟节点。 They cannot be used to target the previous siblings and so if the ::after of each element is used to create the separator then the separator on top can never be hidden. 它们不能用于定位先前的兄弟节点,因此如果使用每个元素的::after来创建分隔符,则永远不会隐藏顶部的分隔符。

Instead, we can use the ::before elements (on all but the first-child ) to create the separator. 相反,我们可以使用::before元素(除了first-child所有元素)来创建分隔符。 In this scenario we can use CSS selectors to hide both the the current element's ::before and the next element's ::before on hover. 在这种情况下,我们可以使用CSS选择器隐藏当前元素的::before和下一个元素::before on hover。

 .menu { float: left; color: white; } .menu > ul { list-style: none; text-align: center; padding: 0; margin: 0; } .menu > ul > li { display: block; position: relative; padding: 60% 5px; background-color: #048990; cursor: pointer; transition: all 0.5s; } .menu > ul > li:hover { background-color: #444; color: white; } .menu > ul > li:hover + li::before, .menu > ul > li:hover::before { opacity: 0; } .menu > ul > li:active { background-color: #444; } .menu >ul >li:not(:first-child)::before { content: ""; background: #FFF; position: absolute; top: 0; left: 7%; height: 1px; width: 86%; } 
 <div class="menu"> <ul> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> </ul> </div> 

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

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