折疊(Collapse)插件可以很容易地讓頁面區(qū)域折疊起來。無論您用它來創(chuàng)建折疊導(dǎo)航還是內(nèi)容面板,它都允許很多內(nèi)容選項(xiàng)。
如果您想要單獨(dú)引用該插件的功能,那么您需要引用 collapse.js。同時(shí),也需要在您的 Bootstrap 版本中引用 Transition(過渡)插件?;蛘?,正如 Bootstrap 插件概覽 一章中所提到,您可以引用 bootstrap.js 或壓縮版的 bootstrap.min.js。
您可以使用折疊(Collapse)插件:
創(chuàng)建可折疊的分組或折疊面板(accordion),如下所示:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 示例 - 折疊面板</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <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"> 點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 1 部分 </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse in"> <div class="panel-body"> Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. </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"> 點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 2 部分 </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse"> <div class="panel-body"> Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree"> 點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 3 部分 </a> </h4> </div> <div id="collapseThree" class="panel-collapse collapse"> <div class="panel-body"> Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. </div> </div> </div> </div> </body> </html>測試看看 ?/?
結(jié)果如下所示:

data-toggle="collapse" 添加到您想要展開或折疊的組件的鏈接上。
href 或 data-target 屬性添加到父組件,它的值是子組件的 id。
data-parent 屬性把折疊面板(accordion)的 id 添加到要展開或折疊的組件的鏈接上。
創(chuàng)建不帶 accordion 標(biāo)記的簡單的可折疊組件(collapsible),如下所示:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 示例 - 簡單的可折疊組件</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo"> 簡單的可折疊組件 </button> <div id="demo" class="collapse in"> Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. </div> </body> </html>測試看看 ?/?
結(jié)果如下所示:

正如您在示例中看到的,我們創(chuàng)建了一個(gè)可折疊的組件,與折疊面板(accordion)不同,我們沒有添加屬性 data-parent。
下表列出了折疊(Collapse)插件用于處理繁重的伸縮的 class:
| Class | 描述 | 示例 |
|---|---|---|
| .collapse | 隱藏內(nèi)容。 | 嘗試一下 |
| .collapse.in | 顯示內(nèi)容。 | 嘗試一下 |
| .collapsing | 當(dāng)過渡效果開始時(shí)被添加,當(dāng)過渡效果完成時(shí)被移除。 |
您可以通過以下兩種方式使用折疊(Collapse)插件:
通過 data 屬性:向元素添加 data-toggle="collapse" 和 data-target,自動(dòng)分配可折疊元素的控制。data-target 屬性接受一個(gè) CSS 選擇器,并會(huì)對其應(yīng)用折疊效果。請確保向可折疊元素添加 class .collapse。如果您希望它默認(rèn)情況下是打開的,請?zhí)砑宇~外的 class .in。
為了向可折疊控件添加類似折疊面板的分組管理,請?zhí)砑?data 屬性 data-parent="#selector"。
通過 JavaScript:可通過 JavaScript 激活 collapse 方法,如下所示:
$('.collapse').collapse()有一些選項(xiàng)是通過 data 屬性或 JavaScript 來傳遞的。下表列出了這些選項(xiàng):
| 選項(xiàng)名稱 | 類型/默認(rèn)值 | Data 屬性名稱 | 描述 |
|---|---|---|---|
| parent | selector 默認(rèn)值:false | data-parent | 如果提供了一個(gè)選擇器,當(dāng)可折疊項(xiàng)目顯示時(shí),指定父元素下的所有可折疊的元素將被關(guān)閉。這與傳統(tǒng)的折疊面板(accordion)的行為類似 - 這依賴于 accordion-group 類。 |
| toggle | boolean 默認(rèn)值:true | data-toggle | 切換調(diào)用可折疊元素。 |
下面是一些折疊(Collapse)插件中有用的方法:
| 方法 | 描述 | 示例 |
|---|---|---|
| Options: .collapse(options) | 激活內(nèi)容為可折疊元素。接受一個(gè)可選的 options 對象。 | $('#identifier').collapse({
toggle: false
}) |
| Toggle: .collapse('toggle') | 切換顯示/隱藏可折疊元素。 | $('#identifier').collapse('toggle') |
| Show: .collapse('show') | 顯示可折疊元素。 | $('#identifier').collapse('show') |
| Hide: .collapse('hide') | 隱藏可折疊元素。 | $('#identifier').collapse('hide') |
下面的示例演示了方法的用法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 示例 - 折疊(Collapse)插件方法</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<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">
點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 1 部分--hide 方法
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice
lomo.
</div>
</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseTwo">
點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 2 部分--show 方法
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice
lomo.
</div>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseThree">
點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 3 部分--toggle 方法
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice
lomo.
</div>
</div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseFour">
點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。第 4 部分--options 方法
</a>
</h4>
</div>
<div id="collapseFour" class="panel-collapse collapse">
<div class="panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice
lomo.
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () { $('#collapseFour').collapse({
toggle: false
})});
$(function () { $('#collapseTwo').collapse('show')});
$(function () { $('#collapseThree').collapse('toggle')});
$(function () { $('#collapseOne').collapse('hide')});
</script>
</body>
</html>測試看看 ?/?結(jié)果如下所示:

下表列出了折疊(Collapse)插件中要用到的事件。這些事件可在函數(shù)中當(dāng)鉤子使用。
| 事件 | 描述 | 示例 |
|---|---|---|
| show.bs.collapse | 在調(diào)用 show 方法后觸發(fā)該事件。 | $('#identifier').on('show.bs.collapse', function () {
// 執(zhí)行一些動(dòng)作...
}) |
| shown.bs.collapse | 當(dāng)折疊元素對用戶可見時(shí)觸發(fā)該事件(將等待 CSS 過渡效果完成)。 | $('#identifier').on('shown.bs.collapse', function () {
// 執(zhí)行一些動(dòng)作...
}) |
| hide.bs.collapse | 當(dāng)調(diào)用 hide 示例方法時(shí)立即觸發(fā)該事件。 | $('#identifier').on('hide.bs.collapse', function () {
// 執(zhí)行一些動(dòng)作...
}) |
| hidden.bs.collapse | 當(dāng)折疊元素對用戶隱藏時(shí)觸發(fā)該事件(將等待 CSS 過渡效果完成)。 | $('#identifier').on('hidden.bs.collapse', function () {
// 執(zhí)行一些動(dòng)作...
}) |
下面的示例演示了事件的用法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 示例 - 折疊(Collapse)插件事件</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="panel-group" id="accordion">
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseexample">
點(diǎn)擊我進(jìn)行展開,再次點(diǎn)擊我進(jìn)行折疊。--shown 事件
</a>
</h4>
</div>
<div id="collapseexample" class="panel-collapse collapse">
<div class="panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson
cred nesciunt sapiente ea proident.
Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#collapseexample').on('show.bs.collapse', function () {
alert('嘿,當(dāng)您展開時(shí)會(huì)提示本警告');})
});
</script>
</body>
</html>測試看看 ?/?結(jié)果如下所示:
