Skip to content

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-right that 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-carousel attribute. The <script> build starts every data-carousel element on the page when the DOM is ready.
  • A carousel that is running gets the class js-carousel-initialized and a tabindex of 0. To reach one that data-carousel started, for example to call next(), use Carousel.get.
  • arrowKeys needs 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.

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);
});

new Carousel(element, settings?, callback?): Carousel

Defined in: components/carousel/carousel.ts:271

HTMLElement

The carousel element.

CarouselOptions

The options. A value that is not valid is replaced by the default.

CarouselCallback

Called after every slide, with the index of the slide and the carousel element.

Carousel

If element is not an element, or settings is not an object.

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.

number

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).

boolean = false

true to start autoplay, false to stop it.

void


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.

CarouselCallback

The function to call.

void

carousel.complete((index, el) => console.log(index, el));

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.

void


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.

number

The index of the slide, counting from 0.

void


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.

void

If the element has no <li class="carousel-slide">.


next(): void

Defined in: components/carousel/carousel.ts:382

Shows the next slide. With infinite the last slide is followed by the first.

void


prev(): void

Defined in: components/carousel/carousel.ts:387

Shows the previous slide. With infinite the first slide is followed by the last.

void


static get(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.

HTMLElement

The carousel element.

Carousel | null

Carousel.get(document.getElementById('animals') as HTMLElement)?.next();