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: Carousel
4
description: A slideshow component for cycling through elements—images or slides of text—like a carousel.
5
group: components
6
toc: true
7
---
8
 
9
## How it works
10
 
11
The carousel is a slideshow for cycling through a series of content, built with CSS 3D transforms and a bit of JavaScript. It works with a series of images, text, or custom markup. It also includes support for previous/next controls and indicators.
12
 
13
In browsers where the [Page Visibility API](https://www.w3.org/TR/page-visibility/) is supported, the carousel will avoid sliding when the webpage is not visible to the user (such as when the browser tab is inactive, the browser window is minimized, etc.).
14
 
15
{{< callout info >}}
16
{{< partial "callout-info-prefersreducedmotion.md" >}}
17
{{< /callout >}}
18
 
19
Please be aware that nested carousels are not supported, and carousels are generally not compliant with accessibility standards.
20
 
21
Lastly, if you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}).
22
 
23
## Example
24
 
25
Carousels don't automatically normalize slide dimensions. As such, you may need to use additional utilities or custom styles to appropriately size content. While carousels support previous/next controls and indicators, they're not explicitly required. Add and customize as you see fit.
26
 
27
**The `.active` class needs to be added to one of the slides** otherwise the carousel will not be visible. Also be sure to set a unique `id` on the `.carousel` for optional controls, especially if you're using multiple carousels on a single page. Control and indicator elements must have a `data-target` attribute (or `href` for links) that matches the `id` of the `.carousel` element.
28
 
29
### Slides only
30
 
31
Here's a carousel with slides only. Note the presence of the `.d-block` and `.w-100` on carousel images to prevent browser default image alignment.
32
 
33
{{< example >}}
34
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
35
  <div class="carousel-inner">
36
    <div class="carousel-item active">
37
        {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
38
    </div>
39
    <div class="carousel-item">
40
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
41
    </div>
42
    <div class="carousel-item">
43
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
44
    </div>
45
  </div>
46
</div>
47
{{< /example >}}
48
 
49
### With controls
50
 
51
Adding in the previous and next controls. We recommend using `<button>` elements, but you can also use `<a>` elements with `role="button"`.
52
 
53
{{< example >}}
54
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
55
  <div class="carousel-inner">
56
    <div class="carousel-item active">
57
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
58
    </div>
59
    <div class="carousel-item">
60
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
61
    </div>
62
    <div class="carousel-item">
63
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
64
    </div>
65
  </div>
66
 <button class="carousel-control-prev" type="button" data-target="#carouselExampleControls" data-slide="prev">
67
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
68
    <span class="sr-only">Previous</span>
69
  </button>
70
  <button class="carousel-control-next" type="button" data-target="#carouselExampleControls" data-slide="next">
71
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
72
    <span class="sr-only">Next</span>
73
  </button>
74
</div>
75
{{< /example >}}
76
 
77
### With indicators
78
 
79
You can also add the indicators to the carousel, alongside the controls, too.
80
 
81
{{< example >}}
82
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
83
  <ol class="carousel-indicators">
84
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
85
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
86
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
87
  </ol>
88
  <div class="carousel-inner">
89
    <div class="carousel-item active">
90
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
91
    </div>
92
    <div class="carousel-item">
93
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
94
    </div>
95
    <div class="carousel-item">
96
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
97
    </div>
98
  </div>
99
  <button class="carousel-control-prev" type="button" data-target="#carouselExampleIndicators" data-slide="prev">
100
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
101
    <span class="sr-only">Previous</span>
102
  </button>
103
  <button class="carousel-control-next" type="button" data-target="#carouselExampleIndicators" data-slide="next">
104
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
105
    <span class="sr-only">Next</span>
106
  </button>
107
</div>
108
{{< /example >}}
109
 
110
### With captions
111
 
112
Add captions to your slides easily with the `.carousel-caption` element within any `.carousel-item`. They can be easily hidden on smaller viewports, as shown below, with optional [display utilities]({{< docsref "/utilities/display" >}}). We hide them initially with `.d-none` and bring them back on medium-sized devices with `.d-md-block`.
113
 
114
{{< example >}}
115
<div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
116
  <ol class="carousel-indicators">
117
    <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
118
    <li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
119
    <li data-target="#carouselExampleCaptions" data-slide-to="2"></li>
120
  </ol>
121
  <div class="carousel-inner">
122
    <div class="carousel-item active">
123
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
124
      <div class="carousel-caption d-none d-md-block">
125
        <h5>First slide label</h5>
126
        <p>Some representative placeholder content for the first slide.</p>
127
      </div>
128
    </div>
129
    <div class="carousel-item">
130
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
131
      <div class="carousel-caption d-none d-md-block">
132
        <h5>Second slide label</h5>
133
        <p>Some representative placeholder content for the second slide.</p>
134
      </div>
135
    </div>
136
    <div class="carousel-item">
137
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
138
      <div class="carousel-caption d-none d-md-block">
139
        <h5>Third slide label</h5>
140
        <p>Some representative placeholder content for the third slide.</p>
141
      </div>
142
    </div>
143
  </div>
144
  <button class="carousel-control-prev" type="button" data-target="#carouselExampleCaptions" data-slide="prev">
145
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
146
    <span class="sr-only">Previous</span>
147
  </button>
148
  <button class="carousel-control-next" type="button" data-target="#carouselExampleCaptions" data-slide="next">
149
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
150
    <span class="sr-only">Next</span>
151
  </button>
152
</div>
153
{{< /example >}}
154
 
155
### Crossfade
156
 
157
Add `.carousel-fade` to your carousel to animate slides with a fade transition instead of a slide. Depending on your carousel content (e.g., text only slides), you may want to add `.bg-body` or some custom CSS to the `.carousel-item`s for proper crossfading.
158
 
159
{{< example >}}
160
<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
161
  <div class="carousel-inner">
162
    <div class="carousel-item active">
163
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
164
    </div>
165
    <div class="carousel-item">
166
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
167
    </div>
168
    <div class="carousel-item">
169
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
170
    </div>
171
  </div>
172
  <button class="carousel-control-prev" type="button" data-target="#carouselExampleFade" data-slide="prev">
173
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
174
    <span class="sr-only">Previous</span>
175
  </button>
176
  <button class="carousel-control-next" type="button" data-target="#carouselExampleFade" data-slide="next">
177
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
178
    <span class="sr-only">Next</span>
179
  </button>
180
</div>
181
{{< /example >}}
182
 
183
### Individual `.carousel-item` interval
184
 
185
Add `data-interval=""` to a `.carousel-item` to change the amount of time to delay between automatically cycling to the next item.
186
 
187
{{< example >}}
188
<div id="carouselExampleInterval" class="carousel slide" data-ride="carousel">
189
  <div class="carousel-inner">
190
    <div class="carousel-item active" data-interval="10000">
191
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
192
    </div>
193
    <div class="carousel-item" data-interval="2000">
194
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
195
    </div>
196
    <div class="carousel-item">
197
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
198
    </div>
199
  </div>
200
  <button class="carousel-control-prev" type="button" data-target="#carouselExampleInterval" data-slide="prev">
201
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
202
    <span class="sr-only">Previous</span>
203
  </button>
204
  <button class="carousel-control-next" type="button" data-target="#carouselExampleInterval" data-slide="next">
205
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
206
    <span class="sr-only">Next</span>
207
  </button>
208
</div>
209
{{< /example >}}
210
 
211
### Disable touch swiping
212
 
213
Carousels support swiping left/right on touchscreen devices to move between slides. This can be disabled using the `data-touch` attribute. The example below also does not include the `data-ride` attribute and has `data-interval="false"` so it doesn't autoplay.
214
 
215
{{< example >}}
216
<div id="carouselExampleControlsNoTouching" class="carousel slide" data-touch="false" data-interval="false">
217
  <div class="carousel-inner">
218
    <div class="carousel-item active">
219
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" >}}
220
    </div>
221
    <div class="carousel-item">
222
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" >}}
223
    </div>
224
    <div class="carousel-item">
225
      {{< placeholder width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" >}}
226
    </div>
227
  </div>
228
  <button class="carousel-control-prev" type="button" data-target="#carouselExampleControlsNoTouching" data-slide="prev">
229
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
230
    <span class="sr-only">Previous</span>
231
  </button>
232
  <button class="carousel-control-next" type="button" data-target="#carouselExampleControlsNoTouching" data-slide="next">
233
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
234
    <span class="sr-only">Next</span>
235
  </button>
236
</div>
237
{{< /example >}}
238
 
239
## Usage
240
 
241
### Via data attributes
242
 
243
Use data attributes to easily control the position of the carousel. `data-slide` accepts the keywords `prev` or `next`, which alters the slide position relative to its current position. Alternatively, use `data-slide-to` to pass a raw slide index to the carousel `data-slide-to="2"`, which shifts the slide position to a particular index beginning with `0`.
244
 
245
The `data-ride="carousel"` attribute is used to mark a carousel as animating starting at page load. If you don't use `data-ride="carousel"` to initialize your carousel, you have to initialize it yourself. **It cannot be used in combination with (redundant and unnecessary) explicit JavaScript initialization of the same carousel.**
246
 
247
### Via JavaScript
248
 
249
Call carousel manually with:
250
 
251
```js
252
$('.carousel').carousel()
253
```
254
 
255
### Options
256
 
257
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-interval=""`.
258
 
259
<table class="table table-bordered table-striped">
260
  <thead>
261
    <tr>
262
      <th style="width: 100px;">Name</th>
263
      <th style="width: 50px;">Type</th>
264
      <th style="width: 50px;">Default</th>
265
      <th>Description</th>
266
    </tr>
267
  </thead>
268
  <tbody>
269
    <tr>
270
      <td>interval</td>
271
      <td>number</td>
272
      <td>5000</td>
273
      <td>The amount of time to delay between automatically cycling an item. If <code>false</code>, carousel will not automatically cycle.</td>
274
    </tr>
275
    <tr>
276
      <td>keyboard</td>
277
      <td>boolean</td>
278
      <td>true</td>
279
      <td>Whether the carousel should react to keyboard events.</td>
280
    </tr>
281
    <tr>
282
      <td>pause</td>
283
      <td>string | boolean</td>
284
      <td>'hover'</td>
285
      <td><p>If set to <code>'hover'</code>, pauses the cycling of the carousel on <code>mouseenter</code> and resumes the cycling of the carousel on <code>mouseleave</code>. If set to <code>false</code>, hovering over the carousel won't pause it.</p>
286
      <p>On touch-enabled devices, when set to <code>'hover'</code>, cycling will pause on <code>touchend</code> (once the user finished interacting with the carousel) for two intervals, before automatically resuming. Note that this is in addition to the above mouse behavior.</p></td>
287
    </tr>
288
    <tr>
289
      <td>ride</td>
290
      <td>string</td>
291
      <td>false</td>
292
      <td>Autoplays the carousel after the user manually cycles the first item. If set to <code>'carousel'</code>, autoplays the carousel on load.</td>
293
    </tr>
294
    <tr>
295
      <td>wrap</td>
296
      <td>boolean</td>
297
      <td>true</td>
298
      <td>Whether the carousel should cycle continuously or have hard stops.</td>
299
    </tr>
300
    <tr>
301
      <td>touch</td>
302
      <td>boolean</td>
303
      <td>true</td>
304
      <td>Whether the carousel should support left/right swipe interactions on touchscreen devices.</td>
305
    </tr>
306
  </tbody>
307
</table>
308
 
309
### Methods
310
 
311
{{< callout danger >}}
312
{{< partial "callout-danger-async-methods.md" >}}
313
{{< /callout >}}
314
 
315
#### `.carousel(options)`
316
 
317
Initializes the carousel with an optional options `object` and starts cycling through items.
318
 
319
```js
320
$('.carousel').carousel({
321
  interval: 2000
322
})
323
```
324
 
325
#### `.carousel('cycle')`
326
 
327
Cycles through the carousel items from left to right.
328
 
329
#### `.carousel('pause')`
330
 
331
Stops the carousel from cycling through items.
332
 
333
#### `.carousel(number)`
334
 
335
Cycles the carousel to a particular frame (0 based, similar to an array). **Returns to the caller before the target item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
336
 
337
#### `.carousel('prev')`
338
 
339
Cycles to the previous item. **Returns to the caller before the previous item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
340
 
341
#### `.carousel('next')`
342
 
343
Cycles to the next item. **Returns to the caller before the next item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
344
 
345
#### `.carousel('dispose')`
346
 
347
Destroys an element's carousel.
348
 
349
#### `.carousel('nextWhenVisible')`
350
 
351
Don't cycle the carousel to next when the page isn't visible or the carousel or its parent isn't visible. **Returns to the caller before the next item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
352
 
353
#### `.carousel('to')`
354
 
355
Cycles the carousel to a particular frame (0 based, similar to an array). **Returns to the caller before the next item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
356
 
357
### Events
358
 
359
Bootstrap's carousel class exposes two events for hooking into carousel functionality. Both events have the following additional properties:
360
 
361
- `direction`: The direction in which the carousel is sliding (either `"left"` or `"right"`).
362
- `relatedTarget`: The DOM element that is being slid into place as the active item.
363
- `from`: The index of the current item
364
- `to`: The index of the next item
365
 
366
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
367
 
368
<table class="table table-bordered table-striped">
369
  <thead>
370
    <tr>
371
      <th style="width: 150px;">Event Type</th>
372
      <th>Description</th>
373
    </tr>
374
  </thead>
375
  <tbody>
376
    <tr>
377
      <td>slide.bs.carousel</td>
378
      <td>This event fires immediately when the <code>slide</code> instance method is invoked.</td>
379
    </tr>
380
    <tr>
381
      <td>slid.bs.carousel</td>
382
      <td>This event is fired when the carousel has completed its slide transition.</td>
383
    </tr>
384
  </tbody>
385
</table>
386
 
387
```js
388
$('#myCarousel').on('slide.bs.carousel', function () {
389
  // do something...
390
})
391
```
392
 
393
### Change transition duration
394
 
395
The transition duration of `.carousel-item` can be changed with the `$carousel-transition` Sass variable before compiling or custom styles if you're using the compiled CSS. If multiple transitions are applied, make sure the transform transition is defined first (eg. `transition: transform 2s ease, opacity .5s ease-out`).