gpt4 book ai didi

javascript - 所有子菜单下拉菜单从顶部显示

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:32 24 4
gpt4 key购买 nike

我试图让子菜单从顶部开始显示,而不是从它应该显示的位置开始,如图所示。我该怎么做?

我不知道这是否需要 Javascript。如果需要,请随意编辑 CSS 或添加 Javascript。

enter image description here

JSFiddle 演示: https://jsfiddle.net/h91kg3s5/

HTML

<div id="main-nav" class="justify-content-end">
<ul id="menu-main-menu" class="navbar-nav">
<li class="menu-item current-menu-item current_page_item menu-item-home active nav-item"><a href="#" class="nav-link">Main Menu 1</a>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item"><a href="#" data-toggle="dropdown" class="dropdown-toggle nav-link">Main Menu 2</a>
<ul class="dropdown-menu" role="menu">
<li class="menu-item menu-item-has-children dropdown nav-item"><a href="#" class="dropdown-item">Menu 1</a>
<ul class="dropdown-menu" role="menu">
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 1</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 2</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 3</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 4</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 5</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 6</a></li>
</ul>
</li>
<li class="menu-item menu-item-has-children dropdown nav-item"><a href="#" class="dropdown-item">Menu 2</a>
<ul class="dropdown-menu" role="menu">
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 11</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 12</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 13</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 14</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 15</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 16</a></li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Sub Menu 17</a></li>
</ul>
</li>
<li class="menu-item nav-item"><a href="#" class="dropdown-item">Menu 3</a>
</li>
</ul>
</li>
<li class="menu-item nav-item"><a href="#" class="nav-link">Main Menu 3</a></li>
<li class="menu-item nav-item"><a href="#" class="nav-link">Main Menu 4</a></li>
</ul>
</div>

CSS:

.navbar-nav {
-ms-flex-direction: row;
flex-direction: row;
}
.navbar-nav .dropdown-menu {
position: absolute;
}
.navbar-nav > li {
margin: 0 15px;
}
.dropdown-menu .dropdown-toggle::after {
border-bottom: 0.3em solid transparent;
border-left: 0.3em solid;
border-top: 0.3em solid transparent;
}
.dropdown-menu.show {
border: 1px solid #999;
background-color: #ffffff;
}
.dropdown.menu-item-has-children .dropdown.menu-item-has-children {
position: relative;
}
.dropdown.menu-item-has-children .dropdown.menu-item-has-children>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
box-shadow: 0 1px 6px 0 rgba(0, 0, 0, 0.11);
background-color: #ffffff;
border: 1px solid #999;
}
.dropdown.menu-item-has-children .dropdown.menu-item-has-children:hover>.dropdown-menu {
display: block;
}

.dropdown.menu-item-has-children .dropdown.menu-item-has-children>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #ccc;
margin-top: 5px;
margin-right: -10px;
}

.dropdown.menu-item-has-children .dropdown.menu-item-has-children:hover>a:after {
border-left-color: #fff;
}

.dropdown.menu-item-has-children .dropdown.menu-item-has-children.pull-left {
float: none;
}

.dropdown.menu-item-has-children .dropdown.menu-item-has-children.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
-webkit-border-radius: 6px 0 6px 6px;
-moz-border-radius: 6px 0 6px 6px;
border-radius: 6px 0 6px 6px;
}

最佳答案

只需删除或取消设置这一行:

.dropdown.menu-item-has-children .dropdown.menu-item-has-children {
position: unset;
}

JSFiddle

如果您从 parent 中删除 relative 属性 -> .dropdown.menu-item-has-children .dropdown.menu-item-has-children 它与具有 relative 属性的父级保持一致,所以你的 ul -> dropdown-menu show 已经有了它!

关于javascript - 所有子菜单下拉菜单从顶部显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58591469/

24 4 0