douji2283 2017-11-27 14:18
浏览 144
已采纳

Bootstrap可防止多个可折叠元素同时打开

I have the following html sidebar menu:

<li {{{ (Request::is('bookings/*') }}} data-toggle="collapse" data-target="#bookings">
    <a href="#" id="bookings-menu-item"><i class="fa fa-address-book" aria-hidden="true"></i> Bookings <i class="fa fa-chevron-down" aria-hidden="true"></i></a>
</li>   
<ul class="sub-menu collapse"  id="bookings">
    <li class="collapsed"><a href="{{ route('bookings') }}">All Bookings</a></li>
    <li class="collapsed" ><a href="{{ route('bookings.create') }}">Add New</a></li>
</ul>

<li {{{ (Request::is('bookings/*') || Request::is('bookings') ? 'class=active' : '') }}} data-toggle="collapse" data-target="#item2">
   <a href="#" id="bookings-menu-item"><i class="fa fa-address-book" aria-hidden="true"></i> Item 2 <i class="fa fa-chevron-down" aria-hidden="true"></i></a>
</li>   
<ul class="sub-menu collapse"  id="item2">
    <li {{{ (Request::is('bookings') ? 'class=active' : 'collapsed') }}}><a href="{{ route('bookings') }}">All Bookings</a></li>
    <li {{{ (Request::is('bookings/create') ? 'class=active' : 'collapsed') }}} ><a href="{{ route('bookings.create') }}">Add New</a></li>
</ul>

This is a basic Bootstrap collapse menu that contains a sub menu which expands when the li element is clicked.

The problem I have is lets say I have 2 or 3 of these menu items that all have sub menus. There is a possibility that all of them could open at the same time, I don't like this because this forces a scroll overflow as the height increases which then shows a scrollbar for the side menu.

Is there a way I can prevent multiple elements from being expanded in Bootstrap?

I am using Laravel 5 if that helps.

enter image description here

  • 写回答

1条回答 默认 最新

  • douyakan8924 2017-11-27 15:59
    关注

    I think this should help you, just play a little with it to adapt it to your needs:

    <div class="row">
        <div class="col">
            <ul class="nav nav-stacked" id="accordion1">
                <li class="panel"> <a data-toggle="collapse" data-parent="#accordion1" href="#firstLink">Test12</a>
    
                    <ul id="firstLink" class="collapse">
                        <li>SubTest1</li>
                        <li>SubTest1</li>
                        <li>SubTest1</li>
                    </ul>
    
                </li>
                <li class="panel"> <a data-toggle="collapse" data-parent="#accordion1" href="#secondLink">Test2</a>
    
                    <ul id="secondLink" class="collapse">
                        <li>SubTest2</li>
                        <li>SubTest2</li>
                        <li>SubTest2</li>
                        <li>SubTest2</li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
    

    See it here

    https://codepen.io/anon/pen/ZaMOxN

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况