Carousel
Slides only
With controls
With indicators
With captions
Crossfade
Individual .carousel-item interval
Disable touch swiping
Dark variant
Usage
Via data attributes
Use data attributes to easily control the position of the carousel. data-bs-slide
accepts the keywords prev
or next
, which alters the slide position relative to its current position. Alternatively, use data-bs-slide-to
to pass a raw slide index to the carousel data-bs-slide-to="2"
, which shifts the slide position to a particular index beginning with 0
.
The data-bs-ride="carousel"
attribute is used to mark a carousel as animating starting at page load. If you don’t use data-bs-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.
Via JavaScript
Call carousel manually with:
const carousel = new bootstrap.Carousel('#myCarousel')
Options
Name | Type | Default | Description |
---|---|---|---|
interval | number | 5000 | The amount of time to delay between automatically cycling an item. |
keyboard | boolean | true | Whether the carousel should react to keyboard events. |
pause | string, boolean | "hover" | If set to "hover" , pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave . If set to false , hovering over the carousel won’t pause it. On touch-enabled devices, when set to "hover" , cycling will pause on touchend (once the user finished interacting with the carousel) for two intervals, before automatically resuming. This is in addition to the mouse behavior. |
ride | string, boolean | false | If set to true , autoplays the carousel after the user manually cycles the first item. If set to "carousel" , autoplays the carousel on load. |
touch | boolean | true | Whether the carousel should support left/right swipe interactions on touchscreen devices. |
wrap | boolean | true | Whether the carousel should cycle continuously or have hard stops. |
Methods
You can create a carousel instance with the carousel constructor, for example, to initialize with additional options and start cycling through items:
const myCarouselElement = document.querySelector('#myCarousel')
const carousel = new bootstrap.Carousel(myCarouselElement, {
interval: 2000,
wrap: false
})
Method | Description |
---|---|
cycle | Cycles through the carousel items from left to right. |
dispose | Destroys an element’s carousel. (Removes stored data on the DOM element) |
getInstance | Static method which allows you to get the carousel instance associated to a DOM element, you can use it like this: bootstrap.Carousel.getInstance(element) . |
getOrCreateInstance | Static method which returns a carousel instance associated to a DOM element or create a new one in case it wasn’t initialized. You can use it like this: bootstrap.Carousel.getOrCreateInstance(element) . |
next | Cycles to the next item. Returns to the caller before the next item has been shown (e.g., before the slid.bs.carousel event occurs). |
nextWhenVisible | Don’t cycle carousel to next when the page isn’t visible or the carousel or its parent isn’t visible. Returns to the caller before the target item has been shown. |
pause | Stops the carousel from cycling through items. |
prev | Cycles to the previous item. Returns to the caller before the previous item has been shown (e.g., before the slid.bs.carousel event occurs). |
to | Cycles the carousel to a particular frame (0 based, similar to an array). Returns to the caller before the target item has been shown (e.g., before the slid.bs.carousel event occurs). |
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.from
: The index of the current itemto
: The index of the next item
All carousel events are fired at the carousel itself (i.e. at the <div class="carousel">
).
Event type | Description |
---|---|
slid.bs.carousel | Fired when the carousel has completed its slide transition. |
slide.bs.carousel | Fires immediately when the slide instance method is invoked. |
const myCarousel = document.getElementById('myCarousel')
myCarousel.addEventListener('slide.bs.carousel', event => {
// do something...
})