Bootstrap 4:如何创建一个带有手风琴的下拉菜单?
Posted
技术标签:
【中文标题】Bootstrap 4:如何创建一个带有手风琴的下拉菜单?【英文标题】:Bootstrap 4: How to create a dropdown menu with an accordion inside it? 【发布时间】:2019-06-22 08:01:10 【问题描述】:实际上,我在让引导手风琴在下拉菜单中工作时遇到了一些问题。 我用谷歌搜索了很多,也在这里找到了一些 *** 线程,但它们已经有几年历史了,并且取决于 bootstrap 3 或 bootstrap 2。 喜欢这个:Twitter Bootstrap: How to create a dropdown button with an accordion inside it?
任何人都可以在这里帮助我或在引导下拉菜单中为手风琴工作提供解决方案。
<nav class="navbar navbar-expand-md">
<ul class="navbar-nav">
<li class="nav-item mega-dropdown-menu">
<a class="nav-link" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Test</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<nav class="navbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria- controls="collapseExample">Accordion</a>
<div id="collapseExample" class="collapse">
<ul class="navbar-nav">
<li class="nav-link">
<a class="nav-link" href="#">Accordion Link 1</a>
</li>
<li class="nav-link">
<a class="nav-link" href="#">Accordion Link 2</a>
</li>
</ul>
</div>
</li>
</ul>
</nav>
</div>
</li>
</ul>
</nav>
【问题讨论】:
【参考方案1】:保持导航栏下拉菜单在点击时打开的技巧是使用<form>
标签。因此,如果您将手风琴放在 <form>
中,它将按需要工作。
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu">
<form class="accordion" id="accordionExample">
<div>
<a class="nav-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Item #1
</a>
<div id="collapseOne" class="collapse px-3 show" data-parent="#accordionExample">
Collapse 1 content
</div>
</div>
<div>
<a class="nav-link" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Item #2
</a>
<div id="collapseTwo" class="collapse px-3" data-parent="#accordionExample">
Collapse 2 content...
</div>
</div>
<div>
<a class="nav-link" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Item #3
</a>
<div id="collapseThree" class="collapse px-3" data-parent="#accordionExample">
Collapse 3 content...
</div>
</div>
</form>
</div>
</li>
</ul>
</div>
演示:https://www.codeply.com/go/F8iQy0MeLa
【讨论】:
您能否为 Bootstrap 5 提供解决方法?【参考方案2】:试试这个
// Prevent dropdown to be closed when we click on an accordion link
$('.dropdown-accordion').on('click', 'a[data-toggle="collapse"]', function (event)
$($(this).attr('href')).collapse('toggle');
event.stopPropagation();
);
<script src="http://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">
<div class="dropdown dropdown-accordion">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Dropdown
</button>
<div id="accordion" class="dropdown-menu">
<div class="card dropdown-item">
<div class="card-header">
<a class="card-link" data-toggle="collapse" href="#collapseOne">
Item #1
</a>
</div>
<div id="collapseOne" class="collapse show" data-parent="#accordion">
<div class="card-body">
Lorem ipsum dolor sit amet
</div>
</div>
</div>
<div class="card dropdown-item">
<div class="card-header">
<a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo">
Item #2
</a>
</div>
<div id="collapseTwo" class="collapse" data-parent="#accordion">
<div class="card-body">
Lorem ipsum dolor sit amet
</div>
</div>
</div>
<div class="card dropdown-item">
<div class="card-header">
<a class="collapsed card-link" data-toggle="collapse" href="#collapseThree">
Item #3
</a>
</div>
<div id="collapseThree" class="collapse" data-parent="#accordion">
<div class="card-body">
Lorem ipsum dolor sit amet
</div>
</div>
</div>
</div>
</div>
</div>
【讨论】:
它不是 100% 工作的。再次单击时无法关闭手风琴。看看这个 codepen -> codepen.io/atcraigwatson/pen/yNGmEm 这个工作得很好,但遗憾的是没有使用 Bootstrap 4 标记。所以我需要的是相同的行为,但在 Bootsrap 4 中以上是关于Bootstrap 4:如何创建一个带有手风琴的下拉菜单?的主要内容,如果未能解决你的问题,请参考以下文章
带有下拉图标-chevron 的 Bootstrap Accordion 导航
Bootstrap手风琴,点击时滚动到活动手风琴的顶部,我将如何在嵌套手风琴上实现?
如何在 HTML/Bootstrap 4 中使用变量作为选择器?