Slider Bootstrap đẹp

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.

In browsers where the Page Visibility API 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.).

Please be aware that nested carousels are not supported, and carousels are generally not compliant with accessibility standards.

Lastly, if you’re building our JavaScript from source, it .

Example

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.

Be sure to set a unique id on the .carousel for optional controls, especially if you’re using multiple carousels on a single page.

Slides only

Here’s a carousel with slides only. Note the presence of the .d-block and


    
    
  

Previous Next

0 on carousel images to prevent browser default image alignment.


    
    
  

With controls

Adding in the previous and next controls:


    
    
  

Previous Next

You can also add the indicators to the carousel, alongside the controls, too.

Previous Next

Initial active element required

The


    
    
  

Previous Next

1 class needs to be added to one of the slides. Otherwise, the carousel will not be visible.

With captions

Add captions to your slides easily with the


    
    
  

Previous Next

2 element within any

    
    
  

Previous Next

3. They can be easily hidden on smaller viewports, as shown below, with optional display utilities. We hide them initially with

    
    
  

Previous Next

4 and bring them back on medium-sized devices with

    
    
  

Previous Next

5.

...

...

Usage

Via data attributes

Use data attributes to easily control the position of the carousel.


    
    
  

Previous Next

6 accepts the keywords

    
    
  

Previous Next

7 or

    
    
  

Previous Next

8, which alters the slide position relative to its current position. Alternatively, use

    
    
  

Previous Next

9 to pass a raw slide index to the carousel

Previous Next

0, which shifts the slide position to a particular index beginning with

Previous Next

1.

The

Previous Next

2 attribute is used to mark a carousel as animating starting at page load. It cannot be used in combination with (redundant and unnecessary) explicit JavaScript initialization of the same carousel.

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

Previous Next

3, as in

Previous Next

4.

NameTypeDefaultDescriptionintervalnumber5000The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.keyboardbooleantrueWhether the carousel should react to keyboard events.pausestring | boolean"hover"

If set to

Previous Next

5, pauses the cycling of the carousel on

Previous Next

6 and resumes the cycling of the carousel on

Previous Next

7. If set to

Previous Next

8, hovering over the carousel won't pause it.

On touch-enabled devices, when set to

Previous Next

5, cycling will pause on
...

...

0 (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.

ridestringfalseAutoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load.wrapbooleantrueWhether the carousel should cycle continuously or have hard stops.

Methods

Asynchronous methods and transitions

All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.

See our JavaScript documentation for more information.

...

...

1

Initializes the carousel with an optional options

...

...

2 and starts cycling through items.

$('.carousel').carousel({
  interval: 2000
})

...

...

3

Cycles through the carousel items from left to right.

...

...

4

Stops the carousel from cycling through items.

...

...

5

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

...

...

6 event occurs).

...

...

7

Cycles to the previous item. Returns to the caller before the previous item has been shown (i.e. before the

...

...

6 event occurs).

...

...

9

Cycles to the next item. Returns to the caller before the next item has been shown (i.e. before the

...

...

6 event occurs).

$('.carousel').carousel()
1

Destroys an element’s carousel.

Events

Bootstrap’s carousel class exposes two events for hooking into carousel functionality. Both events have the following additional properties:

  • $('.carousel').carousel()
    2: The direction in which the carousel is sliding (either
    $('.carousel').carousel()
    3 or
    $('.carousel').carousel()
    4).
  • $('.carousel').carousel()
    5: The DOM element that is being slid into place as the active item.
  • $('.carousel').carousel()
    6: The index of the current item
  • $('.carousel').carousel()
    7: The index of the next item

All carousel events are fired at the carousel itself (i.e. at the

).Event TypeDescriptionslide.bs.carouselThis event fires immediately when the

$('.carousel').carousel()
8 instance method is invoked.slid.bs.carouselThis event is fired when the carousel has completed its slide transition.
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something…
})