我喜欢@joshmoto的解决方案,因为它将子菜单直接对齐在菜单项下,但我认为如果你应用它,你可以让子菜单完全展开
position: static
进入菜单1等列表项。
如果你的子菜单是全宽的,那么你需要将子菜单向右放置,以使内容对齐(你需要根据菜单文本调整值)。
$("#menubar li.dropdown").hover(function() {
$(this).addClass("active show");
},
function() {
$(this).removeClass("active show");
});
$("#menubar ul.dropdown-menu>li>a").on("click", function(e) {
$("#menubar li.dropdown").removeClass("active show");
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<style>
.bg-banner {
background-image: linear-gradient(#17517f, #46769d);
}
.bg-headbar {
background-color: #005db9;
}
.bg-banner .nav-link {
color: white !important;
}
#menubar .nav-link {
font-size: 16px;
padding-right: 50px;
color: white;
}
#menubar .nav-link:focus,
#menubar .nav-link:hover,
#menubar .nav-link:visited {
color: white;
}
@media (min-width:768px) {
.navbar {
height: 2.5rem;
}
nav.navbar .navbar-nav li.nav-item.active:after {
content: "";
position: relative;
display: inline-block;
margin-left: -1.6875rem;
left: 50%;
bottom: 1.1875rem;
border-left: 0.375rem solid transparent;
border-right: 0.375rem solid transparent;
border-bottom: 0.375rem solid white;
}
.navbar-nav li:nth-child(2) .dropdown-menu {
padding-left: 6.75rem;
}
.navbar-nav li:nth-child(3) .dropdown-menu {
padding-left: 13.4375rem;
}
.dropdown-menu>a {
font-weight: 600;
float: left;
width: auto;
clear: none;
}
}
@media (max-width:767px) {
.dropdown-menu>a {
display: block !important;
font-weight: 600;
}
}
#menubar li.dropdown.show {
position: static;
}
#menubar li.dropdown.show .dropdown-menu {
display: table;
width: 100%;
text-align: center;
left: 0;
right: 0;
margin: 0;
}
</style>
<nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
<a class="navbar-brand" href="#">Test Site</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse navbar-toggle">
<form class="form-inline my-2 .m-md-0 ml-auto">
<div class="input-group">
<input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
<div class="input-group-append">
<button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
</div>
<div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
<nav class="navbar navbar-expand navbar-light">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link" data-toggle="dropdown" role="button" href="#">
<i class="fa fa-user"></i>
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Sign Out</a>
</div>
</li>
<li class="nav-item">
<a href="#" class="nav-link"><i class="fa fa-cog"></i></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link" data-toggle="dropdown" role="button" href="#">
<i class="fa fa-question-circle"></i>
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Help</a>
</div>
</li>
</ul>
</nav>
</div>
</nav>
<nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0 py-8-md">
<div class="collapse navbar-collapse navbar-toggle align-self-start" id="menubar">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Menu1
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown1">
<a class="dropdown-item" href="#">Home<span class="sr-only"> (current)</span></a>
<a class="dropdown-item" href="#">Submenu 1B</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Menu2
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown2">
<a class="dropdown-item" href="#">Submenu 2A</a>
<a class="dropdown-item" href="#">Submenu 2B</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Menu3
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown3">
<a class="dropdown-item" href="#">Submenu 3A</a>
<a class="dropdown-item" href="#">Submenu 3B</a>
</div>
</li>
</ul>
</div>
</nav>
<div class="container">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
</div>
因为你没有
dropdown-toggle
如果用户使用tab键协商您的网站,并且子菜单在触摸设备上无法正常工作,则菜单1上的类等项目将无法打开。如果你不需要标签或触摸支持,那么应该没问题。