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

Bootstrap Carousel Plugin

Bootstrap Carousel Plugin

The Bootstrap Carousel plugin is a flexible, responsive way to add a slideshow to your site. In addition, the content is flexible enough to be images, iframes, videos, or any other type of content you want to place.

If you want to reference the plugin's functionality separately, you need to include carousel.js. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。

Example

Below is a simple slideshow that uses the Bootstrap Carousel plugin to display a generic component with cycling elements. To implement the carousel, you only need to add code with the appropriate markup. There is no need to use data attributes; simple class-based development is enough.

Example

<div id="myCarousel" class=">

carousel slide

"

<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>
<div class="carousel-inner">
<div class=">

item active

"

<img src="/wp-content/uploads/2014/07/slide1.png" alt=">

First slide

"

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide2.png" alt=">

Second slide

"

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide3.png" alt=">

Third slide

"

div

div

<a class=">

carousel-control left

"

href

=

"

#myCarousel

"

data-slide

=

"

prev

"

<span_ngcontent-c3="" aria-hidden="true" class=">

glyphicon glyphicon-chevron-right

"

span

a

<a class=">

carousel-control right

"

href

=

"

#myCarousel

"

data-slide

=

"

next

"

›

a

div

Try it »

The result is as follows:

Optional captions

You can add captions to slides through the .item element inside the .carousel-caption element. Simply place any optional HTML there and it will be automatically aligned and formatted. The following example demonstrates this:

Example

<div id="myCarousel" class=">

carousel slide

"

<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>
<div class="carousel-inner">
<div class=">

item active

"

<img src="/wp-content/uploads/2014/07/slide1.png" alt=">

First slide

"

<div class="carousel-caption">

Heading 1

div

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide2.png" alt=">

Second slide

"

<div class="carousel-caption">

Caption 2

div

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide3.png" alt=">

Third slide

"

<div class="carousel-caption">

Caption 3

div

div

div

<a class=">

left carousel-control

"

href

=

"

#myCarousel

"

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

=

"

#myCarousel

"

role

=

"

button

"

data-slide

=

"

next

"

<span class=">

glyphicon glyphicon-chevron-right

"

aria-hidden

=

"

true

"

span

<

span

class

=

"

sr-only

"

Next

span

a

div

Try it »

The result is as follows:

Usage

- The **data-slide** attribute accepts the keyword *prev* or *next*, used to change the position of the slide relative to the current one. - Use **data-slide-to** to pass a raw slide index to the carousel; **data-slide-to="2"** will move the slider to a specific index, counting from 0. - The **data-ride="carousel"** attribute is used to indicate that the carousel starts animating when the page loads.

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
interval number
Default value: 5000 data-interval The amount of time to delay between automatically cycling each item. If false, the carousel will not cycle automatically.
pause string
Default value: "hover" data-pause Pause the carousel cycle when the mouse enters, and resume the carousel cycle when the mouse leaves.
wrap boolean
Default value: true data-wrap Whether the carousel cycles continuously.

Method

The following are some useful methods of the Carousel plugin:

Method Description Example
.carousel(options) Initializes the carousel with an optional options object and starts cycling through the items.

$('#identifier').carousel({ interval: 2000 })| onSelect | event | .carousel('cycle') | Cycles the carousel items from left to right. |

$('#identifier').carousel('cycle')

| onSelect | event | .carousel('pause') | Stops the carousel from cycling items. |
$('#identifier').carousel('pause')
| onSelect | event | .carousel(number) | Cycles the carousel to a particular frame (counting from 0, similar to an array). |
$('#identifier').carousel(number)
| onSelect | event | .carousel('prev') | Cycles the carousel to the previous item. |
$('#identifier').carousel('prev')
| onSelect | event | .carousel('next') | Cycles the carousel to the next item. |
$('#identifier').carousel('next')
|

Example

The following example demonstrates the use of the methods:

Example

<div id="myCarousel" class=">

carousel slide

"

<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>
<div class="carousel-inner">
<div class=">

item active

"

<img src="/wp-content/uploads/2014/07/slide1.png" alt=">

First slide

"

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide2.png" alt=">

Second slide

"

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide3.png" alt=">

Third slide

"

div

div

<a class=">

left carousel-control

"

href

=

"

#myCarousel

"

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

=

"

#myCarousel

"

role

=

"

button

"

data-slide

=

"

next

"

<span class=">

glyphicon glyphicon-chevron-right

"

aria-hidden

=

"

true

"

span

<span class="sr-only" Next span a>
<div style=">

text-align:center;

"

<input type="button" class=">

btn start-slide

"

value

=

"

Start

"

<input type="button" class=">

btn pause-slide

"

value

=

"

Pause

"

<input type="button" class=">

btn prev-slide

"

value

=

"

Previous Slide

"

<input type="button" class=">

btn next-slide

"

value

=

"

Next Slide

"

<input type="button" class=">

btn slide-one

"

value

=

"

Slide 1

"

<input type="button" class=">

btn slide-two

"

value

=

"

Slide 2

"

<input type="button" class=">

btn slide-three

"

value

=

"

Slide 3

"

div

div

<script$(function(){//>

Initialize carousel

$

(

"

.start-slide

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

'

cycle

'

)

;

}

)

;

//

Pause carousel

$

(

"

.pause-slide

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

'

pause

'

)

;

}

)

;

//

Cycle to previous item

$

(

"

.prev-slide

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

'

prev

'

)

;

}

)

;

//

Cycle to next item

$

(

"

.next-slide

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

'

next

'

)

;

}

)

;

//

Cycle to a specific frame

$

(

"

.slide-one

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

0

)

;

}

)

; $

(

"

.slide-two

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

1

)

;

}

)

; $

(

"

.slide-three

"

)

.

click

(

function

(

)

{

$

(

"

#myCarousel

"

)

.

carousel

(

2

)

;

}

)

;

}

)

;

script

Try it »

The result is as follows:

Events

The following table lists the events used in the Carousel plugin. These events can be used as hooks in functions.

Events Description Example
slide.bs.carousel This event is triggered immediately when the slide instance method is called.

$('#identifier').on('slide.bs.carousel', function () { // Do something... })| onSelect | event | slid.bs.carousel | This event is triggered when the carousel has finished the slide transition. |

$('#identifier').on('slid.bs.carousel', function () {

    // Perform some actions...

})

|

Example

The following example demonstrates the use of the events:

Example

<div id="myCarousel" class=">

carousel slide

"

<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>
<div class="carousel-inner">
<div class=">

item active

"

<img src="/wp-content/uploads/2014/07/slide1.png" alt=">

First slide

"

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide2.png" alt=">

Second slide

"

div

<div class="item">
<img src="/wp-content/uploads/2014/07/slide3.png" alt=">

Third slide

"

div

div

<a class=">

left carousel-control

"

href

=

"

#myCarousel

"

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

=

"

#myCarousel

"

role

=

"

button

"

data-slide

=

"

next

"

<span class=">

glyphicon glyphicon-chevron-right

"

aria-hidden

=

"

true

"

span

<span class="sr-only" Next span a div>
<script$(function(){$('#myCarousel' )>

.

on

(

'

slide.bs.carousel

'

,

function

(

)

{

alert

(

"

This event is triggered immediately when the slide instance method is called.

"

)

;

}

)

;

}

)

;

script

Try it »

The result is as follows: