简体   繁体   English

除了将鼠标悬停在div上之外,如何定位同一类别的所有div?

[英]How to target all divs of the same class except for hovered over div?

I have a set of divs all with the same class (they don't have to have the same class if it makes this easier). 我有一组div都具有相同的类(如果这样更容易,则不必具有相同的类)。 Ideally what I want is when a user hovers over one of these divs the other divs (with a background image in each) all turn grey to put focus on the currently hovered overed div. 理想情况下,我想要的是当用户将鼠标悬停在这些div之一上时,其他div(每个都有背景图像)都变成灰色以将焦点放在当前悬停的div上。 If it was the div being hovered over that was changing I would be fine, but I really have no idea how to tackle this. 如果是将div悬停在上面,那会改变,那很好,但是我真的不知道该如何解决。 Some kind of sibling selector? 某种同级选择器? I would prefer to just use css and am happy if the solution is not backwards compatible. 如果解决方案不向后兼容,我宁愿只使用CSS并感到高兴。

Here is my code so far. 到目前为止,这是我的代码。 Thanks in advance! 提前致谢!

.box:hover (SELECT ALL OTHER .BOX) {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-o-filter: grayscale(100%);
-ms-filter: grayscale(100%);
filter: grayscale(100%); }

edit: I realise I could give each box a different class, and then say when box 1 is hovered over, box 2,3,4 etc go grey, and do that for each.. but this seems like a lot of code for something simple. 编辑:我知道我可以给每个框一个不同的类,然后说当框1悬停时,框2、3、4等变灰,然后对每个框进行灰阶..但这似乎是很多代码简单。

The solution that follows will work only if your elements are adjacent, like a navigation bar. 仅当元素相邻时(例如导航栏),以下解决方案才有效。 Maybe that's not your situation, but it can help others. 也许这不是您的情况,但是它可以帮助其他人。 Must say that it is cross-browser CSS2. 必须说这是跨浏览器的CSS2。

Wrap your <div> inside a container: <div>包装在容器中:

<div class="container">
    <div class="target"></div>
    <div class="target"></div>
    <div class="target"></div>
    <div class="target"></div>
    <div class="target"></div>
</div>

And use it to control :hover 并使用它来控制:hover

.target {
    background-color: #444;
}
.container {
    float: left;
}
.container:hover .target {
    background: #bbb;
}
.container .target:hover {
    background: #444;
}

It's working here 这里工作

There isn't a sibling combinator that lets you select all other siblings of another element. 没有同级组合器,您可以选择另一个元素的所有其他同级。 Atomically you would represent this with .box:not(:hover) as shown in other answers, but you can't select all other .box:not(:hover) elements relatively to a .box:hover element in the same parent. 从原子.box:not(:hover)您可以使用.box:not(:hover)来表示它,如其他答案所示,但是相对于同一父对象中的.box:hover元素,您不能选择所有其他.box:not(:hover)元素。

You could do this partway with the sibling combinators + and ~ , but they only look at siblings that follow an element: 您可以使用同级组合符+~ ,但是它们仅查看跟随元素的同级:

.box:hover ~ .box {
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -o-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    filter: grayscale(100%);
}

This means if you hover the 2nd box, then only the 3rd and 4th boxes will gray out but not the 1st. 这意味着,如果将鼠标悬停在第二个框上,则只有第三个和第四个框会变灰,而第一个框则不会变灰。

There isn't a way to select preceding siblings with CSS. 没有办法用CSS选择前面的兄弟姐妹。 Currently the only way to accomplish what you want is to use JavaScript to toggle your styles on the other boxes when hovering a particular box. 当前,完成所需操作的唯一方法是在悬停特定框时使用JavaScript在其他框上切换样式。

Using :not CSS3 selector 使用:not CSS3选择器

div.box:not(:hover) { ... }

if is this what you mean... 如果这是你的意思...

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

相关问题 如何使用 CSS 定位具有相同类的 4 个 div 中的特定 div - How to target a specific div out of 4 divs with the same class with CSS 除了悬停的元素之外,如何设置相同类型的所有元素的样式? - How to style all elements of the same type except the hovered one? 设置变量以针对所有具有相同 Class 的 div - Setting variable to target all divs with same Class 在div悬停时隐藏具有相同名称的所有其他div,显示悬停的div - Hide all other divs with the same name on div hover, show hovered div 将鼠标悬停在一个div上,更改另一div的颜色,但淡出未悬停在其上的所有div - Hover over one div, change color of another but fade out all divs that aren't hovered on 从除其他两个 div 之外的所有 div 中删除 class - Remove class from all div except the other two divs 当两个div在移动设备中一个相对另一个放置时,如何使用CSS滚动悬停的div? - How to scroll the hovered div using CSS when two divs are placed one over the other in mobile? 当子LI悬停在上方时,如何保持除第一个LI之外的所有父项 - How to keep all the parent except the first LI highlighted when the sub LI is hovered over 模糊除悬停之外的所有内容 - Blur all except hovered 在具有相同类别的一组div中将鼠标悬停在单个div上显示覆盖 - Show overlay on hover over single div in set of divs with same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM