Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Collapse
4
description: Toggle the visibility of content across your project with a few classes and our JavaScript plugins.
5
group: components
6
toc: true
7
---
8
 
9
## How it works
10
 
11
The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the `height` from its current value to `0`. Given how CSS handles animations, you cannot use `padding` on a `.collapse` element. Instead, use the class as an independent wrapping element.
12
 
13
{{< callout info >}}
14
{{< partial "callout-info-prefersreducedmotion.md" >}}
15
{{< /callout >}}
16
 
17
## Example
18
 
19
Click the buttons below to show and hide another element via class changes:
20
 
21
- `.collapse` hides content
22
- `.collapsing` is applied during transitions
23
- `.collapse.show` shows content
24
 
25
Generally, we recommend using a button with the `data-target` attribute. While not recommended from a semantic point of view, you can also use a link with the `href` attribute (and a `role="button"`). In both cases, the `data-toggle="collapse"` is required.
26
 
27
{{< example >}}
28
<p>
29
  <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
30
    Link with href
31
  </a>
32
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
33
    Button with data-target
34
  </button>
35
</p>
36
<div class="collapse" id="collapseExample">
37
  <div class="card card-body">
38
    Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
39
  </div>
40
</div>
41
{{< /example >}}
42
 
43
## Horizontal
44
 
45
The collapse plugin also supports horizontal collapsing. Add the `.width` modifier class to transition the `width` instead of `height` and set a `width` on the immediate child element. Feel free to write your own custom Sass, use inline styles, or use our [width utilities]({{< docsref "/utilities/sizing" >}}).
46
 
47
{{< callout info >}}
48
Please note that while the example below has a `min-height` set to avoid excessive repaints in our docs, this is not explicitly required. **Only the `width` on the child element is required.**
49
{{< /callout >}}
50
 
51
{{< example >}}
52
<p>
53
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseWidthExample" aria-expanded="false" aria-controls="collapseWidthExample">
54
    Toggle width collapse
55
  </button>
56
</p>
57
<div style="min-height: 120px;">
58
  <div class="collapse width" id="collapseWidthExample">
59
    <div class="card card-body" style="width: 320px;">
60
      This is some placeholder content for a horizontal collapse. It's hidden by default and shown when triggered.
61
    </div>
62
  </div>
63
</div>
64
{{< /example >}}
65
 
66
## Multiple targets
67
 
68
A `<button>` or `<a>` can show and hide multiple elements by referencing them with a JQuery selector in its `href` or `data-target` attribute.
69
Multiple `<button>` or `<a>` can show and hide an element if they each reference it with their `href` or `data-target` attribute
70
 
71
{{< example >}}
72
<p>
73
  <a class="btn btn-primary" data-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a>
74
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
75
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target=".multi-collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
76
</p>
77
<div class="row">
78
  <div class="col">
79
    <div class="collapse multi-collapse" id="multiCollapseExample1">
80
      <div class="card card-body">
81
        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.
82
      </div>
83
    </div>
84
  </div>
85
  <div class="col">
86
    <div class="collapse multi-collapse" id="multiCollapseExample2">
87
      <div class="card card-body">
88
        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.
89
      </div>
90
    </div>
91
  </div>
92
</div>
93
{{< /example >}}
94
 
95
## Accordion example
96
 
97
Using the [card]({{< docsref "/components/card" >}}) component, you can extend the default collapse behavior to create an accordion. To properly achieve the accordion style, be sure to use `.accordion` as a wrapper.
98
 
99
{{< example >}}
100
<div class="accordion" id="accordionExample">
101
  <div class="card">
102
    <div class="card-header" id="headingOne">
103
      <h2 class="mb-0">
104
        <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
105
          Collapsible Group Item #1
106
        </button>
107
      </h2>
108
    </div>
109
 
110
    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
111
      <div class="card-body">
112
        Some placeholder content for the first accordion panel. This panel is shown by default, thanks to the <code>.show</code> class.
113
      </div>
114
    </div>
115
  </div>
116
  <div class="card">
117
    <div class="card-header" id="headingTwo">
118
      <h2 class="mb-0">
119
        <button class="btn btn-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
120
          Collapsible Group Item #2
121
        </button>
122
      </h2>
123
    </div>
124
    <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
125
      <div class="card-body">
126
        Some placeholder content for the second accordion panel. This panel is hidden by default.
127
      </div>
128
    </div>
129
  </div>
130
  <div class="card">
131
    <div class="card-header" id="headingThree">
132
      <h2 class="mb-0">
133
        <button class="btn btn-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
134
          Collapsible Group Item #3
135
        </button>
136
      </h2>
137
    </div>
138
    <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
139
      <div class="card-body">
140
        And lastly, the placeholder content for the third and final accordion panel. This panel is hidden by default.
141
      </div>
142
    </div>
143
  </div>
144
</div>
145
{{< /example >}}
146
 
147
## Accessibility
148
 
149
Be sure to add `aria-expanded` to the control element. This attribute explicitly conveys the current state of the collapsible element tied to the control to screen readers and similar assistive technologies. If the collapsible element is closed by default, the attribute on the control element should have a value of `aria-expanded="false"`. If you've set the collapsible element to be open by default using the `show` class, set `aria-expanded="true"` on the control instead. The plugin will automatically toggle this attribute on the control based on whether or not the collapsible element has been opened or closed (via JavaScript, or because the user triggered another control element also tied to the same collapsible element). If the control element's HTML element is not a button (e.g., an `<a>` or `<div>`), the attribute `role="button"` should be added to the element.
150
 
151
If your control element is targeting a single collapsible element – i.e. the `data-target` attribute is pointing to an `id` selector – you should add the `aria-controls` attribute to the control element, containing the `id` of the collapsible element. Modern screen readers and similar assistive technologies make use of this attribute to provide users with additional shortcuts to navigate directly to the collapsible element itself.
152
 
153
Note that Bootstrap's current implementation does not cover the various keyboard interactions described in the [ARIA Authoring Practices Guide accordion pattern](https://www.w3.org/WAI/ARIA/apg/patterns/accordion/) - you will need to include these yourself with custom JavaScript.
154
 
155
## Usage
156
 
157
The collapse plugin utilizes a few classes to handle the heavy lifting:
158
 
159
- `.collapse` hides the content
160
- `.collapse.show` shows the content
161
- `.collapsing` is added when the transition starts, and removed when it finishes
162
 
163
These classes can be found in `_transitions.scss`.
164
 
165
### Via data attributes
166
 
167
Just add `data-toggle="collapse"` and a `data-target` to the element to automatically assign control of one or more collapsible elements. 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 `show`.
168
 
169
To add accordion-like group management to a collapsible area, add the data attribute `data-parent="#selector"`. Refer to the demo to see this in action.
170
 
171
### Via JavaScript
172
 
173
Enable manually with:
174
 
175
```js
176
$('.collapse').collapse()
177
```
178
 
179
### Options
180
 
181
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-parent=""`.
182
 
183
<table class="table table-bordered table-striped">
184
  <thead>
185
    <tr>
186
      <th style="width: 100px;">Name</th>
187
      <th style="width: 50px;">Type</th>
188
      <th style="width: 50px;">Default</th>
189
      <th>Description</th>
190
    </tr>
191
  </thead>
192
  <tbody>
193
    <tr>
194
      <td>parent</td>
195
      <td>selector | jQuery object | DOM element </td>
196
      <td>false</td>
197
      <td>If parent is provided, then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the <code>card</code> class). The attribute has to be set on the target collapsible area.</td>
198
    </tr>
199
    <tr>
200
      <td>toggle</td>
201
      <td>boolean</td>
202
      <td>true</td>
203
      <td>Toggles the collapsible element on invocation</td>
204
    </tr>
205
  </tbody>
206
</table>
207
 
208
### Methods
209
 
210
{{< callout danger >}}
211
{{< partial "callout-danger-async-methods.md" >}}
212
{{< /callout >}}
213
 
214
#### `.collapse(options)`
215
 
216
Activates your content as a collapsible element. Accepts an optional options `object`.
217
 
218
```js
219
$('#myCollapsible').collapse({
220
  toggle: false
221
})
222
```
223
 
224
#### `.collapse('toggle')`
225
 
226
Toggles a collapsible element to shown or hidden. **Returns to the caller before the collapsible element has actually been shown or hidden** (i.e. before the `shown.bs.collapse` or `hidden.bs.collapse` event occurs).
227
 
228
#### `.collapse('show')`
229
 
230
Shows a collapsible element. **Returns to the caller before the collapsible element has actually been shown** (i.e. before the `shown.bs.collapse` event occurs).
231
 
232
#### `.collapse('hide')`
233
 
234
Hides a collapsible element. **Returns to the caller before the collapsible element has actually been hidden** (i.e. before the `hidden.bs.collapse` event occurs).
235
 
236
#### `.collapse('dispose')`
237
 
238
Destroys an element's collapse.
239
 
240
### Events
241
 
242
Bootstrap's collapse class exposes a few events for hooking into collapse functionality.
243
 
244
<table class="table table-bordered table-striped">
245
  <thead>
246
    <tr>
247
      <th style="width: 150px;">Event Type</th>
248
      <th>Description</th>
249
    </tr>
250
  </thead>
251
  <tbody>
252
    <tr>
253
      <td>show.bs.collapse</td>
254
      <td>This event fires immediately when the <code>show</code> instance method is called.</td>
255
    </tr>
256
    <tr>
257
      <td>shown.bs.collapse</td>
258
      <td>This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete).</td>
259
    </tr>
260
    <tr>
261
      <td>hide.bs.collapse</td>
262
      <td>This event is fired immediately when the <code>hide</code> method has been called.</td>
263
    </tr>
264
    <tr>
265
      <td>hidden.bs.collapse</td>
266
      <td>This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete).</td>
267
    </tr>
268
  </tbody>
269
</table>
270
 
271
```js
272
$('#myCollapsible').on('hidden.bs.collapse', function () {
273
  // do something...
274
})
275
```