Bootstrap Collapse Plugin
The Collapse plugin makes it easy to collapse areas of a page. Whether you use it to create collapsible navigation or content panels, it allows many content options.
If you want to reference the plugin's functionality separately, you need to include collapse.js. At the same time, you also need to include in your Bootstrap version the Transition plugin. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。
You can use the Collapse plugin to:
- ***Create collapsible groups or an accordion***, as shown below:
Example
Try it »
The result is as follows:
- data-toggle="collapse" Add to the link of the component you want to expand or collapse.
- href or data-target attribute to the parent component, whose value is the child component's id。
- data-parent attribute to add the accordion's id to the link of the component you want to expand or collapse.
- ***Create a simple collapsible component without accordion markup***, as shown below:
Example
Try it »
The result is as follows:
As you can see in the example, we created a collapsible component and, unlike an accordion, we did not add the attribute data-parent。
Usage
The following table lists the classes used by the Collapse plugin to handle the heavy lifting of expanding and collapsing:
| Class | Description | Example |
|---|---|---|
| .collapse | Hides the content. | Try it |
| .collapse.in | Shows the content. | Try it |
| .collapsing | Added when the transition effect starts, and removed when the transition effect completes. |
You can use the Collapse plugin in the following two ways:
- Via data attributes: Add to an element data-toggle="collapse" and data-target, automatically assigning control of the collapsible element.data-target attribute accepts a CSS selector and applies the collapse effect to it. Make sure to add the class .collapseto the collapsible element. If you want it to be open by default, add the extra class .in。
To add accordion-like group management to a collapsible control, add the data attribute data-parent="#selector"。
- via JavaScript: The collapse method can be activated via JavaScript, as shown below:
$('.collapse').collapse()
Option
Some options are passed via data attributes or JavaScript. The following table lists these options:
| Option name | Type / Default value | Data attribute name | Description |
|---|---|---|---|
| parent | selector | ||
| Default value: false | data-parent | If a selector is provided, when a collapsible item is shown, all other collapsible elements under the specified parent will be closed. This is similar to the behavior of a traditional accordion - this relies on the accordion-group class. | |
| toggle | boolean | ||
| Default value: true | data-toggle | Toggles the collapsible element. |
Method
The following are some useful methods of the Collapse plugin:
| Method | Description | Example |
|---|---|---|
| Options: .collapse(options) | Activates the content as a collapsible element. Accepts an optional options object. |
$('#identifier').collapse({ toggle: false })| onSelect | event | Toggle: .collapse('toggle') | Toggles the collapsible element between shown and hidden. |
$('#identifier').collapse('toggle')
| onSelect | event | Show: .collapse('show') | Shows the collapsible element. |
$('#identifier').collapse('show')
| onSelect | event | Hide: .collapse('hide') | Hides the collapsible element. |
$('#identifier').collapse('hide')
|
Example
The following example demonstrates the use of the methods:
Example
<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">Click me to expand, click me again to collapse. Part 1 -- hide method
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">Click me to expand, click me again to collapse. Part 2 -- show method
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">Click me to expand, click me again to collapse. Part 3 -- toggle method
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">Click me to expand, click me again to collapse. Part 4 -- options method
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$(function(){$('#collapseFour' )>.
collapse
(
{
toggle
:
false
}
)
}
)
; $
(
function
(
)
{
$
(
'
#collapseTwo
'
)
.
collapse
(
'
show
'
)
}
)
; $
(
function
(
)
{
$
(
'
#collapseThree
'
)
.
collapse
(
'
toggle
'
)
}
)
; $
(
function
(
)
{
$
(
'
#collapseOne
'
)
.
collapse
(
'
hide
'
)
}
)
;
script
Try it »
The result is as follows:
Events
The following table lists the events used in the Collapse plugin. These events can be used as hooks in functions.
| Events | Description | Example |
|---|---|---|
| show.bs.collapse | This event is triggered after the show method is called. |
$('#identifier').on('show.bs.collapse', function () { // Do something... })| onSelect | event | shown.bs.collapse | This event is triggered when the collapsible element is visible to the user (it will wait for the CSS transition to finish). |
$('#identifier').on('shown.bs.collapse', function () {
// Perform some actions...
})
| onSelect | event | hide.bs.collapse | This event is triggered immediately when the hide instance method is called. |
$('#identifier').on('hide.bs.collapse', function () {
// Perform some actions...
})
| onSelect | event | hidden.bs.collapse | This event is triggered when the collapsible element is hidden from the user (it will wait for the CSS transition to finish). |
$('#identifier').on('hidden.bs.collapse', function () {
// Perform some actions...
})
|
Example
The following example demonstrates the use of the events:
Example
<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">Click me to expand, click me again to collapse. -- shown event
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$(function(){$('#collapseexample' )>.
on
(
'
show.bs.collapse
'
,
function
(
)
{
alert
(
'
Hey, this warning will be shown when you expand
'
)
;
}
)
}
)
;
script
Try it »
The result is as follows:
If you want to reference the plugin's functionality separately, you need to include collapse.js. At the same time, you also need to include in your Bootstrap version