twbs--bootstrap/docs/javascript/collapse.md

194 lines
7.2 KiB
Markdown
Raw Normal View History

2014-07-12 09:20:15 +00:00
---
layout: page
title: Collapse
---
2014-07-13 07:54:21 +00:00
Get base styles and flexible support for collapsible components like accordions and navigation.
2014-03-17 02:03:53 +00:00
2014-07-13 07:54:21 +00:00
<div class="bs-callout bs-callout-danger">
<h4>Plugin dependency</h4>
<p>Collapse requires the <a href="#transitions">transitions plugin</a> to be included in your version of Bootstrap.</p>
</div>
2014-03-17 02:03:53 +00:00
2014-07-13 07:54:21 +00:00
## Example accordion
2014-03-17 02:03:53 +00:00
2014-07-13 07:54:21 +00:00
Using the collapse plugin, we built a simple accordion by extending the panel component.
{% example html %}
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
2014-03-17 02:03:53 +00:00
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
Collapsible Group Item #1
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel">
2014-03-17 02:03:53 +00:00
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</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">
Collapsible Group Item #2
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel">
2014-03-17 02:03:53 +00:00
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</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">
Collapsible Group Item #3
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse" role="tabpanel">
2014-03-17 02:03:53 +00:00
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
</div>
2014-07-13 07:54:21 +00:00
{% endexample %}
You can also use the plugin without the accordion markup. Make a button toggle the expanding and collapsing of another element.
2014-03-17 02:03:53 +00:00
{% highlight html %}
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo">
simple collapsible
</button>
<div id="demo" class="collapse in">...</div>
{% endhighlight %}
2014-07-13 07:54:21 +00:00
## Usage
The collapse plugin utilizes a few classes to handle the heavy lifting:
- `.collapse` hides the content
- `.collapse.in` shows the content
- `.collapsing` is added when the transition starts, and removed when it finishes
These classes can be found in `component-animations.less`.
2014-03-17 02:03:53 +00:00
2014-07-13 07:54:21 +00:00
### Via data attributes
Just add `data-toggle="collapse"` and a `data-target` to element to automatically assign control of a collapsible element. The `data-target` attribute accepts a CSS selector to apply the collapse to. Be sure to add the class `collapse` to the collapsible element. If you'd like it to default open, add the additional class `in`.
To add accordion-like group management to a collapsible control, add the data attribute `data-parent="#selector"`. Refer to the demo to see this in action.
### Via JavaScript
Enable manually with:
2014-03-17 02:03:53 +00:00
{% highlight js %}
$('.collapse').collapse()
{% endhighlight %}
2014-07-13 07:54:21 +00:00
### Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-parent=""`.
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 100px;">Name</th>
<th style="width: 50px;">type</th>
<th style="width: 50px;">default</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>parent</td>
<td>selector</td>
<td>false</td>
<td>If selector then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this dependent on the <code>panel</code> class)</td>
</tr>
<tr>
<td>toggle</td>
<td>boolean</td>
<td>true</td>
<td>Toggles the collapsible element on invocation</td>
</tr>
</tbody>
</table>
</div>
### Methods
#### .collapse(options)
Activates your content as a collapsible element. Accepts an optional options `object`.
2014-03-17 02:03:53 +00:00
{% highlight js %}
$('#myCollapsible').collapse({
toggle: false
})
{% endhighlight %}
2014-07-13 07:54:21 +00:00
#### .collapse('toggle')
Toggles a collapsible element to shown or hidden.
#### .collapse('show')
Shows a collapsible element.
#### .collapse('hide')
Hides a collapsible element.
### Events
Bootstrap's collapse class exposes a few events for hooking into collapse functionality.
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 150px;">Event Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>show.bs.collapse</td>
<td>This event fires immediately when the <code>show</code> instance method is called.</td>
</tr>
<tr>
<td>shown.bs.collapse</td>
<td>This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete).</td>
</tr>
<tr>
<td>hide.bs.collapse</td>
<td>
This event is fired immediately when the <code>hide</code> method has been called.
</td>
</tr>
<tr>
<td>hidden.bs.collapse</td>
<td>This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete).</td>
</tr>
</tbody>
</table>
</div>
2014-03-17 02:03:53 +00:00
{% highlight js %}
$('#myCollapsible').on('hidden.bs.collapse', function () {
// do something…
})
{% endhighlight %}