Bootstrap 4折叠侧边栏必须点击两次
Posted
技术标签:
【中文标题】Bootstrap 4折叠侧边栏必须点击两次【英文标题】:Boostrap 4 collapse sidebar must be clicked twice 【发布时间】:2020-09-12 20:35:33 【问题描述】:我正在尝试创建一个可折叠的左侧菜单,基于那个很好的例子:Codeply example
这里是减少代码:
<div class="row" id="body-row">
<!-- Sidebar -->
<div id="sidebar-container" class="sidebar-expanded d-none d-md-block">
<ul class="list-group"> <!-- Menu with submenu -->
<a href="#submenu1" data-toggle="collapse" aria-expanded="false" class="bg-dark list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-start align-items-center">
<span class="menu-collapsed">Dashboard</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<!-- Submenu content -->
<div id='submenu1' class="collapse sidebar-submenu">
<a href="#" class="list-group-item list-group-item-action bg-dark text-white">
<span class="menu-collapsed">Chahgag</span>
</a>
<a href="#" class="list-group-item list-group-item-action bg-dark text-white">
<span class="menu-collapsed">Reports</span>
</a>
<a href="#" class="list-group-item list-group-item-action bg-dark text-white">
<span class="menu-collapsed">Tables</span>
</a>
</div>
<a href="#submenu2" data-toggle="collapse" aria-expanded="false" class="bg-dark list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-start align-items-center">
<span class="menu-collapsed">Profile</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<!-- Submenu content -->
<div id='submenu2' class="sidebar-submenu">
<a href="#" class="list-group-item list-group-item-action bg-dark text-white">
<span class="menu-collapsed">Settings</span>
</a>
<a href="#" class="list-group-item list-group-item-action bg-dark text-white">
<span class="menu-collapsed">Password</span>
</a>
</div>
</ul><!-- List Group END-->
</div><!-- sidebar-container END -->
<!-- MAIN -->
<div class="col p-4">
Content
</div><!-- Main Col END -->
</div><!-- body-row END -->
CSS
/* Closed submenu icon */
#sidebar-container .list-group .list-group-item[aria-expanded="false"] .submenu-icon::after
content: " \f0d7";
font-family: FontAwesome;
display: inline;
text-align: right;
padding-left: 10px;
/* Opened submenu icon */
#sidebar-container .list-group .list-group-item[aria-expanded="true"] .submenu-icon::after
content: " \f0da";
font-family: FontAwesome;
display: inline;
text-align: right;
padding-left: 10px;
与示例相反,我希望在页面加载时打开菜单。
问题是,如果我删除侧边栏菜单中的“折叠”(在 submenu2 上完成),子菜单会打开,但用户必须单击两次才能折叠它。
这是一个 reduced Codeply 显示问题。
【问题讨论】:
【参考方案1】:在子菜单上添加“show”类而不是删除“collapse”类就可以了。
【讨论】:
【参考方案2】:我使用引导程序 5,与我的情况相同,但我通过将 #
添加到 data-bs-parent 来修复,如下例所示(✔#bar-topics
不是❌bar-topics
)
<ul class="nav flex-column " id="bar-topics">
<li class="nav-item">
<a class="nav-link" data-bs-toggle="collapse" data-bs-target="#m1" href="basic.html"> basic</a>
<ul id="m1" class="submenu collapse" data-bs-parent="#bar-topics" >
....
</ul>
</li>
</ul>
【讨论】:
以上是关于Bootstrap 4折叠侧边栏必须点击两次的主要内容,如果未能解决你的问题,请参考以下文章
vue bootstrap b-collapse:侧边栏折叠时,改变margin-left内容div
带有 jQuery 和 Bootstrap 3 的可折叠响应式侧边栏菜单