简体   繁体   English

CSS:not():悬停选择器

[英]CSS :not():hover selector

<div class="parent">
    <div class="child">child1</div>
    <div class="child">child2</div>
    <div class="child">child3</div>
    <div class="child">child4</div>
    <div class="special">specialChild</div>
</div>

When hovering over one of the .child elements, I want to give all of the child elements of .parent to get another background color. 当鼠标悬停在一个.child元素,我想给所有的子元素的.parent得到另一个背景颜色。 This should not happen when hovering over the .special element. .special悬停在.special元素上时不会发生这种情况。

What I tried so far: 到目前为止我尝试了什么:

.child {
    background-color: rgb(255,0,0);
}
.parent:not(.special):hover .child {
    background-color: rgb(0, 0, 255);
}

But when hovering over the .special element, the background color does change (ignoring the not() selector) 但是当鼠标悬停在.special元素上时,背景颜色会发生变化(忽略not()选择器)

What am I missing? 我错过了什么?

JSFIDDLE: Link JSFIDDLE: 链接

EDIT: jQuery may be used as a solution. 编辑:jQuery可以用作解决方案。

To create a list of items it is better to use the appropriate tags. 要创建项目列表,最好使用适当的标记。

Use the <ul> tag together with the <li> tag to create unordered lists . <ul>标记与<li>标记一起使用以创建无序列表 w3schools.com . - w3schools.com

<ul class="parent">
    <li class="child">child1</li>
    <li class="child">child2</li>
    <li class="child">child3</li>
    <li class="child">child4</li>
    <li class="special">specialChild</li>
</ul>

Then if you want to put the hover only on the items with the class .child you need to specify the class of element that is NOT the specified element for hover. 然后,如果要将悬停仅放在具有类.child的项目上,则需要指定不是悬停指定元素的元素类。

ul.parent li:hover:not(.special) {
    background-color: rgb(0, 0, 255);
}

jsFiddle 的jsfiddle

You can not use .parent:not(.special) because in this way you are selecting all ul.parents except ul.special (but there is no a parent with that class). 不能使用.parent:not(.special)因为这样你是选择所有ul.parents除了ul.special (但没有与该类父)。 The correct way would be: .parent li:not(.special) . 正确的方法是: .parent li:not(.special)

Wrap the child elements into an additional div and apply to it a css rule, eg: child元素包装到另一个div并将css规则应用于它,例如:

<div class="parent">
    <div class="children">
        <div class="child">child1</div>
        <div class="child">child2</div>
        <div class="child">child3</div>
        <div class="child">child4</div>
    </div>
    <div class="special">specialChild</div>
</div>

.children:hover .child {
    background: grey;
}

http://jsfiddle.net/UeZNB/1/ http://jsfiddle.net/UeZNB/1/

You are using the not selector on the parent element, which obviously will never have the class special , the correct way would be: 你正在使用父元素上的not选择器,它显然永远不会有special的类,正确的方法是:

.parent:hover > div:not(.special) {
  background: papayawhip;
}

But because you are using classes, wouldn't be simpler just .parent:hover .child { ... } ? 但是因为你正在使用类,所以不会更简单.parent:hover .child { ... }

EDIT: maybe I misunderstood the question, probably could be asked better. 编辑:也许我误解了这个问题,可能会被问得更好。 However, is something like this you are trying to achieve? 但是,你想要实现这样的事情吗?

You could make the special div absolute positioning, then disable pointer events on it. 您可以进行特殊div绝对定位,然后禁用其上的指针事件。

   .child {
        background-color: red;
    }
    .parent:hover .child {
        background-color:blue;
    }
    .special
    {
        position: absolute;
        z-index:1;  
        pointer-events:none;
    }

FIDDLE 小提琴

JQuery solution. JQuery解决方案。

Create a new class for the hover state you want and toggle it using toggleClass 为所需的悬停状态创建一个新类,并使用toggleClass切换

JSFiddle 的jsfiddle

JQuery JQuery的

(function($) {
    $('.child').hover(function() {
        $( ".child" ).siblings( ".child" ).toggleClass('hovered');
    });
})(jQuery);

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

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