简体   繁体   English

将鼠标悬停在仅显示下拉菜单上

[英]Only show dropdown menu on hovering the parent li

I am trying to build a dropdown menu. 我正在尝试建立一个下拉菜单。 My problem is that hovering the content also shows the dropdown menu. 我的问题是,悬停内容还会显示下拉菜单。 I am currently using this code: 我目前正在使用此代码:

 /* actual dropdown menu */ #menu-hauptmenue { height: 45px; } .sub-menu { opacity: 0; background: #4d4d4d; display: block; z-index: 200; width: 250px; position: absolute; margin-top: 23px; } .sub-menu li { display: block; width: 250px; line-height: 2.7; padding: 10px 15px 10px 15px; z-index: 220; color: #fff; font-family: 'Roboto', sans-serif; font-size: 1em; } .sub-menu li a { color: #fff; font-family: 'Roboto', sans-serif; font-size: 1em; text-decoration: none; } .sub-menu li:hover { background-color: #333333; cursor: pointer; } .sub-menu a:hover { border-bottom: none; } #menu-hauptmenue li:hover .sub-menu { opacity: 1; } #billboard img { z-index: 1; } li { z-index: 20; } .clearfix:after { display: block; clear: both; } nav>ul>li { position: relative; } nav.open .sub-menu { padding-top: 0px; display: none; } /* styling of the nav */ nav ul { list-style: none; display: flex; flex-direction: row; justify-content: space-between; } nav ul li { display: inline-block; } nav ul li a { color: #4D4D4D; font-family: 'Roboto', sans-serif; font-size: 1em; text-decoration: none; } nav ul li a:hover { border-bottom: 1px solid #4D4D4D; } 
 <nav> <ul id="menu-hauptmenue"> <li><a href="#">Parent 1</a> <ul class="sub-menu"> <li><a href="#">Dropdown 1</a></li> <li><a href="#">Dropdown 2</a></li> <li><a href="#">Dropdown 3</a></li> </ul> </li> <li><a href="#">Parent 2</a></li> <li><a href="#">Parent 3</a></li> </ul> </nav> 

As you can see, hovering the content below the navigation also displays the dropdown menu. 如您所见,将内容悬停在导航下方也会显示下拉菜单。 It would be better to only display the navigation when hovering the parent li element, in this case 'Parent 1'. 最好只在将父li元素(在本例中为“ Parent 1”)上悬停时显示导航。 How can I achieve this the best way? 我如何才能达到最佳效果? Thank you for your advice. 感谢您的意见。

It's because your element is present with 0 opacity so you are able to interact with it. 这是因为元素的不透明度0,因此您可以与其进行交互。 You may try using display:none/block like this: 您可以尝试使用display:none / block,如下所示:

 /* actual dropdown menu */ #menu-hauptmenue { height: 45px; } .sub-menu { opacity: 0; background: #4d4d4d; display: none; width: 250px; position: absolute; margin-top: 23px; } .sub-menu li { display: block; width: 250px; line-height: 2.7; padding: 10px 15px 10px 15px; z-index: 220; color: #fff; font-family: 'Roboto', sans-serif; font-size: 1em; } .sub-menu li a { color: #fff; font-family: 'Roboto', sans-serif; font-size: 1em; text-decoration: none; } .sub-menu li:hover { background-color: #333333; cursor: pointer; } .sub-menu a:hover { border-bottom: none; } #menu-hauptmenue li:hover .sub-menu { opacity: 1; display:block; } #billboard img { z-index: 1; } li { z-index: 20; } .clearfix:after { display: block; clear: both; } nav>ul>li { position: relative; } nav.open .sub-menu { padding-top: 0px; display: none; } /* styling of the nav */ nav ul { list-style: none; display: flex; flex-direction: row; justify-content: space-between; } nav ul li { display: inline-block; } nav ul li a { color: #4D4D4D; font-family: 'Roboto', sans-serif; font-size: 1em; text-decoration: none; } nav ul li a:hover { border-bottom: 1px solid #4D4D4D; } 
 <nav> <ul id="menu-hauptmenue"> <li><a href="#">Parent 1</a> <ul class="sub-menu"> <li><a href="#">Dropdown 1</a></li> <li><a href="#">Dropdown 2</a></li> <li><a href="#">Dropdown 3</a></li> </ul> </li> <li><a href="#">Parent 2</a></li> <li><a href="#">Parent 3</a></li> </ul> </nav> 

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

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