Carousel
Defined in: components/carousel/carousel.ts:244
Cycles through images, text and other elements, like a carousel.
- The markup is a list,
<ul>, and each slide is an<li class="carousel-slide">. There is no markup to write for the navigation: the Next and Previous links and the dots are made for you. - The page’s CSS sizes the slides. Each slide has to be as wide as the carousel, with a
margin-rightthat is the gap between two slides. The widths are measured once, when the carousel starts. - Nested carousels are not supported, and the carousel does not meet accessibility standards.
- Start one with JavaScript, or with a
data-carouselattribute. The<script>build starts everydata-carouselelement on the page when the DOM is ready. - A carousel that is running gets the class
js-carousel-initializedand atabindexof0. To reach one thatdata-carouselstarted, for example to callnext(), use Carousel.get. arrowKeysneeds the focus to be on the carousel or on an element inside it.- The carousel makes its own markup from the slides: the slides are made again, so a listener bound to something inside a slide is lost. Carousel.destroy puts the original markup back.
| Event | Fired when |
|---|---|
carousel.init |
the carousel has started |
carousel.next |
Carousel.next moves to the next slide |
carousel.prev |
Carousel.prev moves to the previous slide |
carousel.slide |
a slide starts to move |
carousel.slide.end |
a slide has finished moving |
The events are fired on document. event.detail is { carousel, options }: the element, and the settings
with currentSlideIndex. A swipe and the arrow keys fire carousel.next or carousel.prev, and autoplay
and the dots only fire carousel.slide and carousel.slide.end.
Examples
Section titled “Examples”Markup, started with data-carousel and the default settings
<div class="carousel" data-carousel> <ul> <li class="carousel-slide"><img alt="" src="giraffe.jpg"></li> <li class="carousel-slide"><img alt="" src="eagle.jpg"></li> <li class="carousel-slide"><img alt="" src="lion.jpg"></li> </ul></div>Settings in data-carousel are name and value pairs, separated by colons
<div class="carousel" data-carousel="autoplay:true:dots:true:infinite:true">...</div>Starting a carousel with JavaScript
const carousel = new Carousel( document.getElementById('animals') as HTMLElement, { dots: true, arrowKeys: true, infinite: true }, (index, el) => { // After each slide. `index` is the slide it is on. console.log(index, el); });
carousel.init();Using a carousel that data-carousel started
const carousel = Carousel.get(document.getElementById('animals') as HTMLElement);
carousel?.complete((index) => console.log('now on slide', index));carousel?.next();Listening for events
document.addEventListener('carousel.slide.end', (event) => { const { carousel, options } = (event as CustomEvent<CarouselEventDetail>).detail; console.log(carousel, options.currentSlideIndex);});Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Carousel(
element,settings?,callback?):Carousel
Defined in: components/carousel/carousel.ts:271
Parameters
Section titled “Parameters”element
Section titled “element”HTMLElement
The carousel element.
settings?
Section titled “settings?”The options. A value that is not valid is replaced by the default.
callback?
Section titled “callback?”Called after every slide, with the index of the slide and the carousel element.
Returns
Section titled “Returns”Carousel
Throws
Section titled “Throws”If element is not an element, or settings is not an object.
Accessors
Section titled “Accessors”currentSlideIndex
Section titled “currentSlideIndex”Get Signature
Section titled “Get Signature”get currentSlideIndex():
number
Defined in: components/carousel/carousel.ts:280
The index of the slide the carousel is on, counting from 0. It is 0 when the carousel is not running.
Returns
Section titled “Returns”number
Methods
Section titled “Methods”autoplay()
Section titled “autoplay()”autoplay(
start?):void
Defined in: components/carousel/carousel.ts:417
Starts or stops autoplay. Starting it again starts the wait again. After the last slide it goes back to
the first (all the way back, unless infinite is on).
Parameters
Section titled “Parameters”start?
Section titled “start?”boolean = false
true to start autoplay, false to stop it.
Returns
Section titled “Returns”void
complete()
Section titled “complete()”complete(
callback):void
Defined in: components/carousel/carousel.ts:439
Sets the function that is called after every slide, with the index of the slide and the carousel element. It replaces the one set before, and runs before the callback of the constructor.
Parameters
Section titled “Parameters”callback
Section titled “callback”The function to call.
Returns
Section titled “Returns”void
Example
Section titled “Example”carousel.complete((index, el) => console.log(index, el));destroy()
Section titled “destroy()”destroy():
void
Defined in: components/carousel/carousel.ts:447
Stops the carousel, removes its listeners and puts the original markup back. Calling it for a carousel that is not running does nothing. Carousel.init starts it again.
Returns
Section titled “Returns”void
goTo()
Section titled “goTo()”goTo(
index):void
Defined in: components/carousel/carousel.ts:397
Shows a slide. Nothing happens for an index that is not a slide, for the slide that is showing, or while
a slide is moving. It does not fire carousel.next or carousel.prev.
Parameters
Section titled “Parameters”number
The index of the slide, counting from 0.
Returns
Section titled “Returns”void
init()
Section titled “init()”init():
void
Defined in: components/carousel/carousel.ts:311
Builds the carousel and starts it. The width of the carousel is measured here. Calling it again starts
from scratch, and so does calling it for an element that another Carousel is running on.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If the element has no <li class="carousel-slide">.
next()
Section titled “next()”next():
void
Defined in: components/carousel/carousel.ts:382
Shows the next slide. With infinite the last slide is followed by the first.
Returns
Section titled “Returns”void
prev()
Section titled “prev()”prev():
void
Defined in: components/carousel/carousel.ts:387
Shows the previous slide. With infinite the first slide is followed by the last.
Returns
Section titled “Returns”void
staticget(element):Carousel|null
Defined in: components/carousel/carousel.ts:295
The carousel that is running on an element, or null if there is none. This is how to reach a carousel
that a data-carousel attribute started.
Parameters
Section titled “Parameters”element
Section titled “element”HTMLElement
The carousel element.
Returns
Section titled “Returns”Carousel | null
Example
Section titled “Example”Carousel.get(document.getElementById('animals') as HTMLElement)?.next();