简体   繁体   English

将鼠标悬停在嵌入的div上并更改另一个

[英]hover over an embedded div and change another one

Here is my html: 这是我的HTML:

    <div id="menu">
        <div class="arrows">
            <div class="arrow-down"></div>
        </div>
        <div class="buttons">
            <button class="home">Home</button>
            <button class="about">About</button>
        </div>
    </div>
    <div class="titleHome">
        <p1>Home</p1>
        <div id="bubble"></div>
        <div id="tri"></div>
    </div>

Notice that .home is embedded within 2 divs; 请注意.home嵌入在2个div中; .menu and then .buttons . .menu然后.buttons The display of .titleHome is none . .titleHome的显示为none I want to change the display to block for .titleHome when hovering over .home using pure css. 我想在使用纯css将.titleHome悬停在.home时将显示更改为block .titleHome The problem is I don't know the rules for doing so. 问题是我不知道这样做的规则。

I know that if #b is after #a in the HTML then we can use + . 我知道如果#b在HTML中的#a之后,那么我们可以使用+

I also know that if there are elements in between we can use ~ . 我也知道如果中间有元素我们可以用~

But do these rules apply to .home because it is embedded within other divs. 但这些规则是否适用于.home因为它嵌入在其他div中。 I tried using ~ but with no success. 我试过用~但没有成功。

Can someone please give me a sample as to how to do this? 有人可以给我一个如何做到这一点的样本吗?

At the moment, there are only 3 connecting selectors in CSS: + for the immediate next sibling, ~ for any next sibling and > for a child. 目前,CSS中只有3个连接选择器: +用于下一个兄弟姐妹, ~用于任何下一个兄弟姐妹, >用于孩子。 You cannot access the parent element with plain CSS. 您无法使用纯CSS访问父元素。 Therefore you cannot go one level up and to the next element. 因此,你无法升级到下一个元素。

You can use JavaScript to achieve your requirements without changing your HTML. 您可以使用JavaScript来实现您的要求,而无需更改HTML。 You can also adapt your HTML and have the element, which needs to have another style on the same level as (and after) the hovered element. 您还可以调整HTML并使用元素,该元素需要在与hovered元素相同的级别上(和之后)具有其他样式。

for info , but not usefull at all here, pointer-events to none on adjacent and to auto on childs could do the trick here: 对于信息 ,但在这里根本没用,指针 - 邻居上的无事件和子节点上的自动事件可以在这里做到这一点:

 #menu { pointer-events: none; padding: 1em; } .buttons button.home { pointer-events: auto; cursor: pointer; margin: 1em; } .titleHome { display: none; } #menu:hover + .titleHome { display: block; } 
 <div id="menu"> <div class="arrows"> <div class="arrow-down"></div> </div> <div class="buttons"> <button class="home">Home</button> <button class="about">About</button> </div> </div> <div class="titleHome"> <p1>Home</p1> <div id="bubble"></div> <div id="tri"></div> </div> 

for one single interaction and basic styling it could be used with care. 对于单一的交互和基本样式,它可以小心使用。

for demo or quick test (before writing a script) it can help too ... just do not forget to erase or comment the rules. 对于演示或快速测试(在编写脚本之前)它也可以帮助...只是不要忘记删除或评论规则。

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

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