引导3仍然在RC,但我只是试图实现它。我不知道怎么放一个子菜单类。甚至css中没有类,甚至新的文档也没有提到它
2分钟就到了。用类名作为下拉菜单
引导3仍然在RC,但我只是试图实现它。我不知道怎么放一个子菜单类。甚至css中没有类,甚至新的文档也没有提到它
2分钟就到了。用类名作为下拉菜单
当前回答
引导5(2023年更新)
添加一些JavaScript来防止子菜单在父菜单下拉菜单打开时关闭。这可以通过切换显示:块…
let dropdowns = document.querySelectorAll('.dropdown-toggle')
dropdowns.forEach((dd)=>{
dd.addEventListener('click', function (e) {
var el = this.nextElementSibling
el.style.display = el.style.display==='block'?'none':'block'
})
})
引导5多级下拉单击 引导5多级下拉-悬停 Bootstrap 5多级下拉-悬停&动画过渡
或者,你可以使用这个CSS方法导航栏下拉菜单…
.dropdown-submenu {
position: relative;
}
.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -1px;
}
.navbar-nav li:hover > ul.dropdown-menu {
display: block;
}
引导5导航栏下拉悬停子菜单(仅CSS)
引导4(2018年更新)
在Bootstrap 3 RC中,下拉菜单被移除。用Bootstrap作者Mark Otto的话来说。
“现在子菜单在网络上并没有太大的位置,尤其是在移动网络上。它们将在3.0中被删除”- https://github.com/twbs/bootstrap/pull/6342
但是,用一点额外的CSS,你可以得到相同的功能。
悬停时导航栏子菜单:
.navbar-nav li:hover > ul.dropdown-menu {
display: block;
}
.dropdown-submenu {
position:relative;
}
.dropdown-submenu>.dropdown-menu {
top:0;
left:100%;
margin-top:-6px;
}
导航栏子菜单下拉悬停 导航栏子菜单下拉悬停(右对齐) 导航栏子菜单下拉单击(右对齐) 导航栏下拉悬停(无子菜单)
引导3
下面是一个使用3.0 RC 1的例子:http://bootply.com/71520
下面是一个使用Bootstrap 3.0.0 (final)的示例:http://bootply.com/86684
CSS
.dropdown-submenu {
position:relative;
}
.dropdown-submenu>.dropdown-menu {
top:0;
left:100%;
margin-top:-6px;
margin-left:-1px;
-webkit-border-radius:0 6px 6px 6px;
-moz-border-radius:0 6px 6px 6px;
border-radius:0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display:block;
}
.dropdown-submenu>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:#cccccc;
margin-top:5px;
margin-right:-10px;
}
.dropdown-submenu:hover>a:after {
border-left-color:#ffffff;
}
.dropdown-submenu.pull-left {
float:none;
}
.dropdown-submenu.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;
}
样品的标记
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="menu-item dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Drop Down<b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="menu-item dropdown dropdown-submenu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Level 1</a>
<ul class="dropdown-menu">
<li class="menu-item ">
<a href="#">Link 1</a>
</li>
<li class="menu-item dropdown dropdown-submenu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Level 2</a>
<ul class="dropdown-menu">
<li>
<a href="#">Link 3</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
附注-在导航栏中调整左侧位置的例子:http://bootply.com/92442
其他回答
直到今天(2014年1月9日)Bootstrap 3仍然不支持子菜单下拉。
我搜索了谷歌关于响应式导航菜单,发现这是最好的。
它是智能菜单http://www.smartmenus.org/
我希望这是谁想要导航菜单多级子菜单的出路。
智能菜单现在完全支持子菜单的Bootstrap元素样式。欲了解更多信息,请查看智能菜单网站。
这是我的解决方案(我使用bootstrap 5):
HTML
<div class="btn-group dropend" role="group">
<button id="submenu" type="button" class="dropdown-item dropdown-toggle submenu" data-bs-toggle="dropdown" aria-expanded="false">
Menu Item
</button>
<div class="dropdown-menu" aria-labelledby="submenu">
<a class="dropdown-item">Submenu</a>
</div>
</div>
JS
$('.submenu').click(function(e){
e.preventDefault();
e.stopPropagation();
});
HTML
<!DOCTYPE html>
<html>
<head>
<title>Dropdown Navbar</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">The Providers</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li ><a href="#">Home</a></li>
<li id="course" class="dropdown" ><a href="#">Courses<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">SEO</a></li>
<li><a href="#">A.I</a></li>
<li><a href="#">M.L</a></li>
<li><a href="#">Graphics</a></li>
<li id="sub-dropdown" class="dropdown"><a href="#">web Design<span style="margin-left: 43px;" class="glyphicon glyphicon-chevron-right"></span> </a>
<ul id="sub-dropdown-menu" class="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">Bootstrap 3</a></li>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Angular</a></li>
</ul>
</li>
</li>
</ul>
</li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Contact us</a></li>
<li><a href="#">Policy</a></li>
</ul>
</div>
</div>
</nav>
</body>
</html>
CSS
body{
font-family: monospace;
outline: none;
background-image: url(image.jpg);
background-size: cover;
background-repeat: no-repeat;
height: 100vh;
}
.navbar-inverse{
background-color: #2a84eb ;
border-color: transparent;
}
.navbar-inverse .navbar-brand{
color: #fff;
font-size: 30px;
margin-right: 40px;
}
#myNavbar .navbar-nav li a{
color: #fff;
font-size: 19px;
}
#myNavbar #course{
transition: all 0.3s ease-in-out;
}
#myNavbar #course:hover{
background-color:#0751a6;
}
.dropdown-menu{
display: none;
position: absolute;
background-color: #f0f0f0;
box-shadow: 2px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
#sub-dropdown-menu{
display: none;
position: absolute;
background-color: #f0f0f0;
margin-left: 186px;
margin-top: -26px;
}
#sub-dropdown-menu a , .dropdown-menu a {
color: #000 !important;
font-size: 16px !important;
}
.dropdown:hover .dropdown-menu {display: block;transition: all 0.5s ease-in-out;}
.dropdown .dropdown-menu a:hover{background-color: #c9c7c7;}
#sub-dropdown:hover #sub-dropdown-menu {display: block;}
来源: https://www.youtube.com/watch?v=6CgnWhJ-amE
Shprink的代码对我帮助最大,但为了避免下拉菜单离开屏幕,我将其更新为:
JS:
$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
// Avoid following the href location when clicking
event.preventDefault();
// Avoid having the menu to close when clicking
event.stopPropagation();
// If a menu is already open we close it
$('ul.dropdown-menu [data-toggle=dropdown]').parent().removeClass('open');
// opening the one you clicked on
$(this).parent().addClass('open');
var menu = $(this).parent().find("ul");
var menupos = $(menu).offset();
if (menupos.left + menu.width() > $(window).width()) {
var newpos = -$(menu).width();
menu.css({ left: newpos });
} else {
var newpos = $(this).parent().width();
menu.css({ left: newpos });
}
});
CSS: FROM background-color: #eeeeee TO background-color: #c5c5c5 -白色字体和浅色背景看起来不太好。
.nav .open > a,
.nav .open > a:hover,
.nav .open > a:focus {
background-color: #c5c5c5;
border-color: #428bca;
}
我希望这能像帮助我一样帮助别人!
但是我希望Bootstrap能尽快把subs功能加回来。
我给出了下拉的另一个解。希望这对你有帮助 只需添加这个js脚本
<script type="text/javascript"> jQuery("document").ready(function() {
jQuery("ul.dropdown-menu > .dropdown.parent").click(function(e) {
e.preventDefault();
e.stopPropagation();
if (jQuery(this).hasClass('open2'))
jQuery(this).removeClass('open2');
else {
jQuery(this).addClass('open2');
}
});
}); < /script>
<style type="text/css">.open2{display:block; position:relative;}</style>