简体   繁体   English

悬停影响不是兄弟姐妹的元素

[英]Hover affecting elements that aren't siblings

Following situation: 以下情况:
I have three divs inside a container. 我在容器内有三个div。 If I hover one of these divs the other two should change their appearance. 如果我徘徊其中一个div,另外两个应该改变它们的外观。
Here is a fiddle showing my current situation: 这是一个显示我目前情况的小提琴:
http://jsfiddle.net/SUBHUMAN/gb63t8sb/ http://jsfiddle.net/SUBHUMAN/gb63t8sb/
As you can see it only affects the elements that appear after the hovered element in the DOM. 正如您所看到的,它只影响DOM中悬停元素之后出现的元素。

Is there any way to achieve the wanted behaviour preferably without using JavaScript? 有没有办法实现想要的行为,最好不使用JavaScript?

HTML: HTML:

<div id="container">

    <div id="one">
    </div>

    <div id="two">
    </div>

    <div id="three">
    </div>
</div>


CSS: CSS:

#one {
    width:30px;
    height:30px;
    border:1px solid red;
}

#two {
    width:30px;
    height:30px;
    border:1px solid red;
}

#three {
    width:30px;
    height:30px;
    border:1px solid red;
}

#one:hover ~ div {
    background-color:black;
}

#two:hover ~ div {
    background-color:blue;
}

#three:hover ~ div {
    background-color:purple;
}

The problem is that currently CSS does not support previous siblings selector, so your attempt with general sibling selector ( ~ ) will lead only to selection of next siblings of the hovered element. 问题是当前CSS不支持以前的兄弟选择器,因此您使用通用兄弟选择器~ )的尝试将仅导致选择剩余元素的下一个兄弟。

However, you can simulate the behaviour with the following: 但是,您可以使用以下方法模拟行为:

#container:hover > :not(:hover) {
    background-color: blue;
}

DEMO: http://jsfiddle.net/gb63t8sb/4/ 演示: http //jsfiddle.net/gb63t8sb/4/

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

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