简体   繁体   English

CSS:将鼠标悬停在子选择器上

[英]CSS :hover on child selector

I have this HTML: 我有这个HTML:

<div class="container-s">
    <div class="service">
        <div class="animate-this">

        </div>
    </div>
</div>

The animate-this class could appear different times throughout the html. 动画类在整个html中可能出现不同的时间。 so, this is what I've come up with 所以,这就是我想出的

.container-s .service:hover .container-s .animate-this {

}

But this won't work 但这行不通

Can anyone please suggest an alternative way alongside the child selectors? 有人可以在子选择器旁边建议其他方法吗?

In this specific case, because both elements are part of the same container you do not need to specify the same container more than once: 在这种特定情况下,由于两个元素都是同一容器的一部分,因此您无需多次指定同一容器:

.container-s .service:hover .animate-this

This finds .animate-this within .service on hover, itself within .container-s . 这会在.service上的悬停状态下( .container-s内)找到.animate-this By having the additional .container-s there you are saying that .animate-this appears within some inner container, which is incorrect. 通过使用附加的.container-s ,您可以说.animate-this出现在某个内部容器中,这是不正确的。 This is not to be confused with grouping several distinct selectors using a comma, in which case you will need to repeat the container portion for each selector. 这不要与使用逗号将几个不同的选择器分组在一起混淆,在这种情况下,您将需要为每个选择器重复容器部分。 In this case, you're only dealing with one selector. 在这种情况下,您只需要处理一个选择器。


If .animate-this can appear in a different container than .service , then this would only be possible in CSS if you could actually express this relationship using a selector. 如果.animate-this可以出现在与.service不同的容器中,那么只有在您实际上可以使用选择器表达这种关系的.service ,这才可能在CSS中实现。 Unfortunately CSS selectors are very limited in their scope, in particular there is no way to ascend from a container in order to traverse to a completely different element. 不幸的是,CSS选择器的范围非常有限,特别是无法从容器中升起以遍历到完全不同的元素。

Your real objective is to animate this element when .service is hovered regardless of where it appears in the HTML . 您真正的目标是在.service悬停时.service进行动画处理, 无论它在HTML中的位置如何 This would require using JavaScript to find each element separately, as CSS selectors operate based on concrete relationships between elements. 这将需要使用JavaScript来分别查找每个元素,因为CSS选择器根据元素之间的具体关系进行操作。

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

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