Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Collapse Plugin

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:

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 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"> Click me to expand, click me again to collapse. Part 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"> Click me to expand, click me again to collapse. Part 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>

Try it »

The result is as follows:

Accordion
  1. data-toggle="collapse" Add to the link of the component you want to expand or collapse.
  2. href or data-target attribute to the parent component, whose value is the child component's id。
  3. data-parent attribute to add the accordion's id to the link of the component you want to expand or collapse.
1. Add **data-toggle="collapse"** to the link of the component you want to expand or collapse. 2. Add the **href** or **data-target** attribute to the parent component, whose value is the child component's *id*. 3. The **data-parent** attribute adds the accordion's id to the link of the component you want to expand or collapse.

Example

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo"> Simple collapsible component 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>

Try it »

The result is as follows:

Simple collapsible widget

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:

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: