The Bootstrap carousel is a flexible, responsive way to slideshow component for cycling through elements.
How to Use Bootstrap Carousel
To implement the carousel, you just need to add the code with the markup. There is no need for data attributes, just simple class-based development.
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel" data-slide-to="0" class="active"></li>
<li data-target="#carousel" data-slide-to="1"></li>
<li data-target="#carousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="..." alt="...">
<div class="carousel-caption">
...
</div>
</div>
<div class="item">
<img src="..." alt="...">
<div class="carousel-caption">
...
</div>
</div>
...
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Optional Captions
Add captions to your slides easily with the .carousel-caption
element within any .item
. Place just about any optional HTML within there and it will be automatically aligned and formatted.
<div id = "myCarousel" class = "carousel slide"> <!-- Carousel indicators --> <ol class = "carousel-indicators"> <li data-target = "#myCarousel" data-slide-to = "0" class = "active"></li> <li data-target = "#myCarousel" data-slide-to = "1"></li> <li data-target = "#myCarousel" data-slide-to = "2"></li> </ol> <!-- Carousel items --> <div class = "carousel-inner"> <div class = "item active"> <img src = "/bootstrap/images/slide1.png" alt = "First slide"> <div class = "carousel-caption">This Caption 1</div> </div> <div class = "item"> <img src = "/bootstrap/images/slide2.png" alt = "Second slide"> <div class = "carousel-caption">This Caption 2</div> </div> <div class = "item"> <img src = "/bootstrap/images/slide3.png" alt = "Third slide"> <div class = "carousel-caption">This Caption 3</div> </div> </div> <!-- Carousel nav --> <a class = "carousel-control left" href = "#myCarousel" data-slide = "prev">‹</a> <a class = "carousel-control right" href = "#myCarousel" data-slide = "next">›</a>+ </div>
Multiple carousels
Carousels require the use of an id on the outermost container (the .carousel) for carousel controls to function properly. When adding multiple carousels, or when changing a carousel’s id, be sure to update the relevant controls.
Via data attributes
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.
The data-ride=”carousel” attribute is used to mark a carousel as animating starting at page load.
Via JavaScript
Call carousel manually with:
$('.carousel').carousel()
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-interval=””.
Option Name | Type/Default Value | Data attribute name | Description |
---|---|---|---|
interval | number Default − 5000 | data-interval | The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle. |
pause | string Default − “hover” | data-pause | Pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave. |
wrap | boolean Default − true | data-wrap | Whether the carousel should cycle continuously or have hard stops. |
Methods
.carousel(options)
Initializes the carousel with an optional options object and starts cycling through items.
$('.carousel').carousel({ interval: 2000 })
Method | Description | Example |
---|---|---|
.carousel(options) | Initializes the carousel with an optional options object and starts cycling through items. |
$('#identifier').carousel({ interval: 1000 }) |
.carousel(‘cycle’) | Cycles through the carousel items from left to right. |
$('#identifier').carousel('cycle') |
.carousel(‘pause’) | Stops the carousel from cycling through items. |
$('#identifier')..carousel('pause') |
.carousel(number) | Cycles the carousel to a particular frame (0 based, similar to an array). |
$('#identifier').carousel(number) |
.carousel(‘prev’) | Cycles to the previous item. |
$('#identifier').carousel('prev') |
.carousel(‘next’) | Cycles to the next item. |
$('#identifier').carousel('next') |
Events
Bootstrap’s carousel class exposes two events for hooking into carousel functionality.
Both events have the following additional properties:
direction: The direction in which the carousel is sliding (either “left” or “right”).
relatedTarget: The DOM element that is being slid into place as the active item.
All carousel events are fired at the carousel itself (i.e. at the
).
$('#myCarousel').on('slide.bs.carousel', function () { // do something… })
Event | Description | Example |
---|---|---|
slide.bs.carousel | This event fires immediately when the slide instance method is invoked. |
$('#identifier').on('slide.bs.carousel', function () { // do something }) |
slid.bs.carousel | This event is fired when the carousel has completed its slide transition. |
$('#identifier').on('slid.bs.carousel', function () { // do something }) |
Thank you very much for explaining how the plugin works, I’ll install it on my page and see if I can make it work!
greetings
Thanks for your help, it has helped me to fix my web
Gracias por la información, voy a probarlo ahora mismo en mi tienda online.
In Carousel Module, when I select Order by Publish Date, this only shows first few published articles, although I want this to show last few published articles. Can I set the order or display as reverse of Publishing Date?