简体   繁体   English

在悬停元素时更改列表(ul)的所有其他元素li的css属性

[英]Change css property of all the other elements li of a list (ul) while hovering an element

I really want to know if it's possible to change a property of all the other elements of a list while hovering one element of it. 我真的想知道是否可以在悬停其中一个元素的同时更改列表中所有其他元素的属性。

Let's say that when I hover the "element 2", I want the others: "element 1", "element 3" and "element 4" css color property to change to "red" but not the hovered one. 让我们说当我悬停“元素2”时,我希望其他人:“元素1”,“元素3”和“元素4”css颜色属性变为“红色”而不是悬停的那个。

Is it something possible just with css? 有没有可能只用css? is there a css selector for that actual feature? 那个实际的功能有一个css选择器吗?

 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> <ul> 

You can change the color of all li children by using the pseudo class :hover on the parent ul . 您可以使用伪类更改所有li子项的颜色:hover在父项ul

Then you just need to override the child currently hovered and set it back to the original color: 然后你只需要覆盖当前悬停的孩子并将其设置回原始颜色:

 ul:hover > li { color: red; } ul > li:hover { color: black; } 
 <ul> <li>Element 1</li> <li>Element 2</li> <li>Element 3</li> <li>Element 4</li> </ul> 

You can try by using parent selector 您可以尝试使用父选择器

ul:hover li { color:red; }
ul li:hover { color:white;} // that should be default color

Can't traverse up the DOM with CSS but for your example you can fake it like this.. 无法使用CSS遍历DOM,但对于您的示例,您可以像这样伪造它。

 #list { background-color: gray; list-style-type: none; padding: 0; } #list:hover { background-color: red; } .element:hover { background-color: gray; } 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 2</li> <li class="element">element 4</li> </ul> 

fiddle 小提琴

https://jsfiddle.net/Hastig/u4w0z4sn/ https://jsfiddle.net/Hastig/u4w0z4sn/

Here's another way to fake it (affect an element that is above it on the page, but not in the dom). 这是伪造它的另一种方式(影响页面上方的元素,但不影响dom)。 It doesn't apply in this case but may be worth mentioning for future readers. 它不适用于这种情况,但对于未来的读者可能值得一提。

Yes it's possible with :not negation pseudo class. 是的,它有可能:不是否定伪类。

 #list > .element:not(:hover) { color: red; } 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> <ul> 

Try this 尝试这个

<ul id="list">
    <li class="element">element 1</li>
    <li class="element">element 2</li>
    <li class="element">element 2</li>
    <li class="element">element 4</li>
<ul>
<style>
#list:hover{
    color:red;
}
 .element:hover {
    color: black; /* Replace black with Default color */
}
</style>

This worked for me 这对我有用

The shortest and most elegant solution uses the :not selector : 最短和最优雅的解决方案使用:not selector

 #list:hover .element:not(:hover) {color: red} 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> <ul> 

It's possible to target all siblings that follow the element you're currently hovering over using the general sibling selector ( ~ ) : 这是有可能的目标是跟着你目前正在上空盘旋使用的元素所有兄弟一般兄弟选择( ~

 ul > li:hover ~ li { color: red; } 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> </ul> 

You can also use the adjacent sibling combinator ( + ) to target the sibling element that immediately follows the element you're currently hovering over: 您还可以使用相邻的同级组合子( +来定位紧跟在当前悬停的元素之后的同级元素:

 ul > li:hover + li { color: red; } 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> </ul> 

Unfortunately it is not possible to target previous siblings, so you cannot target element 2 and highlight element 1 , for example. 遗憾的是, 无法定位以前的兄弟姐妹,因此您无法定位element 2并突出显示element 1

However , one solution might be to instead apply the hover to the <ul> , and simply 'reset' the hovered <li> to default with color: inherit : 但是 ,一种解决方案可能是将hover应用于<ul> ,并简单地将hovered <li> '重置'为默认color: inherit

 ul:hover > li { color: red; } ul:hover > li:hover { color: inherit; } 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> </ul> 

Another solution (and perhaps the most elegant) would be to make use of the :not pseudo-class : 另一个解决方案(也许是最优雅的)是使用:not pseudo-class

 ul > li:not(:hover) { color: red; } 
 <ul id="list"> <li class="element">element 1</li> <li class="element">element 2</li> <li class="element">element 3</li> <li class="element">element 4</li> </ul> 

Keep in mind that :not is not supported in Internet Explorer 8 and earlier. 请记住:not Internet Explorer 8及更早版本不支持。

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

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