Collapse作用:
折叠,如左侧各级菜单栏折叠。
Collapse工作原理:
将元素的高度变为0(感兴趣的可以去看源码)
Collapse使用:
class="collapse" 将要折叠的内容声明为collapse类
通过按钮控制折叠(非按钮如链接a可以声明role="button")
Collapse选项:(处于按钮中)
data-bs-toggle="collapse" (绑定collapse事件)
data-bs-target="#target" (折叠的目标,可用id选择器或class选择器)
aria-expanded="false" (true为默认展开,false为默认折叠)
aria-controls="target" (控制的元素,可填多个id,空格隔开)
(处于折叠元素中)
data-bs-parent="#parent" (父元素,组管理功能,如点击展开二级目录其他目录折叠)
示例:
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target=".multi-collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
</p>
<div class="row">
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample2">
<div class="card card-body">
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
下面是我自己试验用的,使用JS将JSON生成菜单,生成的HTML代码如:
<div id="parent">
<div id="first_menu">
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#second_menu" aria-expanded="false" aria-controls="second_menu" type="button">一级菜单</button></h2>
<div class="p-5 collapse second_menu" id="second_menu" data-bs-parent="#first_menu">
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_1" aria-expanded="false" aria-controls="third_menu_1" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_1" data-bs-parent="#second_menu">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_2" aria-expanded="false" aria-controls="third_menu_2" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_2" data-bs-parent="#second_menu">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_3" aria-expanded="false" aria-controls="third_menu_3" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_3" data-bs-parent="#second_menu">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#second_menu1" aria-expanded="false" aria-controls="second_menu1" type="button">一级菜单</button></h2>
<div class="p-5 collapse second_menu" id="second_menu1" data-bs-parent="#first_menu">
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_11" aria-expanded="false" aria-controls="third_menu_11" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_11" data-bs-parent="#second_menu1">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_21" aria-expanded="false" aria-controls="third_menu_21" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_21" data-bs-parent="#second_menu1">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_31" aria-expanded="false" aria-controls="third_menu_31" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_31" data-bs-parent="#second_menu1">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#second_menu2" aria-expanded="false" aria-controls="second_menu2" type="button">一级菜单</button></h2>
<div class="p-5 collapse second_menu" id="second_menu2" data-bs-parent="#first_menu">
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_12" aria-expanded="false" aria-controls="third_menu_12" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_12" data-bs-parent="#second_menu2">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_22" aria-expanded="false" aria-controls="third_menu_22" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_22" data-bs-parent="#second_menu2">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
<h2><button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#third_menu_32" aria-expanded="false" aria-controls="third_menu_32" type="button">二级菜单</button></h2>
<div class="p-5 collapse third_menu" id="third_menu_32" data-bs-parent="#second_menu2">
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
<h2><button class="btn btn-primary">三级菜单</button></h2>
</div>
</div>
</div>
</div>