简体   繁体   English

悬停子元素时保持父元素的悬停状态

[英]Keep hovering state of parent element when hovering child element

my Menu is something like this: 我的菜单是这样的:

<section class="top-bar-section">
 <ul class="left">
  <li class="menu-item menu-item-main-menu menu-item-cine-suntem has-dropdown not-click" id="menu-item-60"><a href="#">TopLevel Menu</a>
   <ul class="dropdown">
    <li class="menu-item menu-item-main-menu menu-item-misiuneviziune" id="menu-item-66"><a href="#">SubLevel Menu</a></li>
   </ul>
</ul>

And the CSS that ads a black arrow when I hover over the TopLevel Menu 当我将鼠标悬停在“顶层菜单”上时,会显示黑色箭头的CSS

.top-bar-section .has-dropdown > a:hover:after{
        content: "";
        display: block;
        width: 0;
        height: 0;
        border: inset 5px;
        border-color: #000 transparent transparent transparent;
        border-top-style: solid;
        margin-top: 22px;
        z-index:999;
      }

How do I get that arrow to stay visible, when I hover the child element of that menu item, because now as soon as I start hovering the child, the arrow from the parent item disappears. 当我将鼠标悬停在菜单项的子元素上时,如何使该箭头保持可见状态,因为现在当我开始将鼠标悬停在该子项上时,父项中的箭头就会消失。

Use the hover for list-item ie li:hover to achieve what you are looking for, as that will select the parent to have the hover that is visible on hover only for the child elements. 将鼠标悬停在list-itemli:hover即可实现所需的功能,因为它将选择父对象,使鼠标悬停仅对子元素可见。

PS: Your above markup as provided is jagged and not correct. PS:您提供的上述标记是锯齿状的,不正确。 You need to correct it for the code to work properly. 您需要更正它以使代码正常工作。

Change 更改

.top-bar-section .has-dropdown > a:hover:after{...}
.top-bar-section li a:hover, .top-bar-section .dropdown li a:hover{background:#c1212e;}

to

.top-bar-section .has-dropdown:hover > a:after {...}
.top-bar-section li:hover a, .top-bar-section .dropdown li:hover a{background:#c1212e;}

Note: 注意:

Closing tags are missing in your HTML markup. HTML标记中缺少结束标记。 Which is a bad practice. 这是一个坏习惯。

DEMO here. 演示在这里。

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

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