在Twitter Bootstrap中,我正在尝试创建一个带有手风琴的按钮下拉列表.
目的是以简明的形式呈现一长串项目(即避免用户长时间向下滚动).
在dropdown div中包含手风琴div的简单方法不起作用..:
<div class="dropdown"> <a data-toggle="dropdown" href="#">Dropdown trigger</a> <div class="dropdown-menu" role="menu" aria-labelledby="dLabel" id="accordion"> <h3>Unit 1</h3> <div> <p>item a</p> <p>item b</p> </div> <h3>Unit 2</h3> <div> <p>item c</p> <p>item d</p> <p>item e</p> </div> </div> </div>
任何想法如何实现这一点?
解决方法
您可以使用以下方法在下拉列表中插入手风琴:
> Bootstrap Dropdown component
> Bootstrap Collapse component
您需要添加几行JS以防止Dropdown事件破坏崩溃事件.
<div class="dropdown dropdown-accordion" data-accordion="#accordion"> <a data-toggle="dropdown" href="#" class="btn btn-primary">Dropdown trigger <span class="caret"></span></a> <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> <li> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapSEOne"> Unit 1 </a> </h4> </div> <div id="collapSEOne" class="panel-collapse collapse"> <div class="panel-body"> <a href="#">Item a</a><br> <a href="#">Item b</a> </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo"> Unit 2 </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse"> <div class="panel-body"> <a href="#">Item a</a><br> <a href="#">Item b</a><br> <a href="#">Item c</a> </div> </div> </div> </div> </li> </ul> </div>
.dropdown-accordion .panel-heading { padding: 0; } .dropdown-accordion .panel-heading a { display: block; padding: 10px 15px; }
// Collapse accordion every time dropdown is shown $('.dropdown-accordion').on('show.bs.dropdown',function (event) { var accordion = $(this).find($(this).data('accordion')); accordion.find('.panel-collapse.in').collapse('hide'); }); // Prevent dropdown to be closed when we click on an accordion link $('.dropdown-accordion').on('click','a[data-toggle="collapse"]',function (event) { event.preventDefault(); event.stopPropagation(); $($(this).data('parent')).find('.panel-collapse.in').collapse('hide'); $($(this).attr('href')).collapse('show'); })