Skip to content

Carousel

Cycle through images, text and other elements, like a carousel.

  • The markup is a list, <ul>, and each slide is an <li class="carousel-slide">. The Next and Previous links and the dots are made for you.
  • Start a carousel with a data-carousel attribute, or with JavaScript.
  • Next and Previous, dots, the arrow keys, swiping, autoplay, and infinite, which goes from the last slide to the first, are all settings.
  • You can listen for the events a carousel fires, and give it a function to call after every slide.
  • Nested carousels are not supported.
HTML
<div class="carousel-container">
<div class="carousel animals" id="demo-carousel">
<ul>
<li class="carousel-slide"><span>0</span><img alt="A giraffe" src="/img/carousel/giraffe.jpg"></li>
<li class="carousel-slide"><span>1</span><img alt="An eagle" src="/img/carousel/eagle.jpg"></li>
<li class="carousel-slide"><span>2</span><img alt="An elephant" src="/img/carousel/elephant.jpg"></li>
<li class="carousel-slide"><span>3</span><img alt="A lion" src="/img/carousel/lion.jpg"></li>
<li class="carousel-slide"><span>4</span><img alt="A tiger" src="/img/carousel/tiger.jpg"></li>
<li class="carousel-slide"><span>5</span><img alt="A wolf" src="/img/carousel/wolf.jpg"></li>
</ul>
</div>
<div class="carousel-status">
<div class="carousel-buttons">
<button id="carousel-prev">Prev</button>
<button id="carousel-next">Next</button>
<button id="carousel-autoplay-start">Autoplay START</button>
<button id="carousel-autoplay-stop">Autoplay STOP</button>
</div>
<p>This carousel is started with JavaScript. It has dots and arrow keys, and it is infinite.</p>
<p>A function is called after every slide, and shows the current index: <span id="carousel-index">0</span></p>
</div>
</div>
<div class="carousel-container">
<div class="carousel animals" data-carousel="autoplay:true:dots:true">
<ul>
<li class="carousel-slide"><img alt="A giraffe" src="/img/carousel/giraffe.jpg"></li>
<li class="carousel-slide"><img alt="An eagle" src="/img/carousel/eagle.jpg"></li>
<li class="carousel-slide"><img alt="An elephant" src="/img/carousel/elephant.jpg"></li>
<li class="carousel-slide"><img alt="A lion" src="/img/carousel/lion.jpg"></li>
<li class="carousel-slide"><img alt="A tiger" src="/img/carousel/tiger.jpg"></li>
<li class="carousel-slide"><img alt="A wolf" src="/img/carousel/wolf.jpg"></li>
</ul>
</div>
<div class="carousel-status">
<p>This carousel is started by its <code>data-carousel</code> attribute. It has dots, and autoplay.</p>
</div>
</div>
JavaScript
// The first carousel is started here: its element, its settings, and a function that is called after every slide.
// The second one needs no script: the script build starts every element that has data-carousel.
var carousel = new ML.Carousel(
document.getElementById('demo-carousel'),
{ dots: true, arrowKeys: true, infinite: true },
function (index) {
document.getElementById('carousel-index').textContent = index;
}
);
document.getElementById('carousel-next').addEventListener('click', function () {
carousel.next();
});
document.getElementById('carousel-prev').addEventListener('click', function () {
carousel.prev();
});
document.getElementById('carousel-autoplay-start').addEventListener('click', function () {
carousel.autoplay(true);
});
document.getElementById('carousel-autoplay-stop').addEventListener('click', function () {
carousel.autoplay(false);
});
carousel.init();

Both carousels have the same markup. The first is started with JavaScript, the second by its data-carousel attribute.

The library’s CSS only lines the slides up in a row and hides the ones that are not showing. The slides’ size, and the look of the links and the dots, are yours. The demo’s are in this site’s CSS.

<div class="carousel" data-carousel="dots:true">
<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>
.carousel { max-width: 300px; }
/* Each slide is as wide as the carousel, with a margin-right for the gap between two slides. */
.carousel-slide { width: 300px; margin-right: 10px; flex: none; }
/* The dots have a size (8px, from the library) but no color. */
.carousel-dots a { background: #ccc; }
.carousel-dots .active a { background: #000; }
/* A link that cannot be used: Previous on the first slide, Next on the last. */
.carousel .inactive { color: #ccc; }

Style the slides by their class, .carousel-slide, and not by li: the dots are <li> too.

data-carousel="autoplay:true:dots:true" sets the options as name:value pairs separated by colons. With JavaScript, new ML.Carousel(element, settings, callback) takes them as an object.

Setting Default What it is
current 0 the slide to start on, counting from 0. A number past the last starts on the last
autoplay false go to the next slide by itself, every autoplaySpeed
autoplaySpeed 2000 how long autoplay waits, in ms
dots false a dot for each slide
nav true the Next and Previous links
arrowKeys false the left and right arrow keys change the slide
touch false swipe to change the slide
infinite false go from the last slide to the first, and from the first to the last

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

var carousel = new ML.Carousel(
document.getElementById('animals'),
{ dots: true, arrowKeys: true, infinite: true },
function (index, el) {
// After every slide. `index` is the slide it is on.
console.log(index, el);
}
);
carousel.init();
carousel.next();
carousel.prev();
carousel.goTo(3);
carousel.autoplay(true);
carousel.destroy();

To reach a carousel that data-carousel started, ask for the one on its element with ML.Carousel.get():

var carousel = ML.Carousel.get(document.getElementById('animals'));
carousel.complete(function (index, el) {
console.log('now on slide', index);
});
Method What it does
init() builds the carousel, and starts it. The width is measured here
next(), prev() show the next or the previous slide
goTo(index) shows a slide. Nothing happens for an index that is not a slide, for the slide that is showing, or while a slide moves
autoplay(start) starts autoplay with true, stops it with false
complete(fn) fn(index, el) is called after every slide, before the function you gave the constructor
destroy() stops the carousel, removes its listeners, and puts the original markup back
currentSlideIndex the slide it is on
Carousel.get(element) the carousel that is running on an element, or null. For one that data-carousel started

Fired on document. event.detail is { carousel, options }: the element, and the settings with currentSlideIndex, the slide the carousel is on.

Event Fired when
carousel.init the carousel has started
carousel.next next() moves to the next slide
carousel.prev prev() moves to the previous slide
carousel.slide a slide starts to move
carousel.slide.end a slide has finished moving

A swipe and the arrow keys call next() and prev(), so they fire carousel.next and carousel.prev. Autoplay, the dots and goTo() only fire carousel.slide and carousel.slide.end.

document.addEventListener('carousel.slide.end', function (event) {
console.log(event.detail.carousel, event.detail.options.currentSlideIndex);
});
  • Each slide has to be as wide as the carousel, with a margin-right that is the gap between two slides: the carousel moves the list by that much. It measures the width, and reads the gap from the first slide, when it starts. If the window is resized and the carousel gets another width, the slides no longer line up. destroy() and init() measure again.
  • It makes its own markup from the slides, so a listener that is bound to something inside a slide is lost when it starts. destroy() puts the original markup back.
  • The carousel gets a tabindex of 0, so that it can have the focus. arrowKeys only works when the focus is on the carousel or on something inside it. With more than one carousel, the keys change the one that has the focus.
  • Anything that changes the slide stops autoplay: a link, a dot, a key, a swipe, and next(), prev() and goTo(). There is no button that pauses it. Autoplay moves without being asked, so use it with care: it is hard to use for people who need more time.
  • With touch, a swipe cancels the touchstart event, so that the browser does not scroll the page. A swipe of more than 100px changes the slide, and a shorter one snaps back. Swiping with a mouse is not supported.
  • The Next link comes before the Previous link in the markup, and so in the order of the keyboard.
  • If the page turns transitions off (transition: none), the carousel finishes each slide at once.
  • The carousel does not meet accessibility standards: the slides have no labels or roles, and the dots are links.

See the generated Carousel class reference for every method and setting.

A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.

  • Your own links and dots. The Next and Previous links and the dots are made by the carousel, in English. Passing your own elements, or labels, would allow buttons, icons and other languages.
  • Any markup for the slides. The slides are <li class="carousel-slide"> in a <ul>. A setting that names the slides would allow other markup.
  • Resizing. The width is measured once, when the carousel starts, so the slides no longer line up after the window is resized.
  • Dragging with the mouse. Swipe is touch only.
  • Touch and scrolling. A swipe cancels touchstart, so the page cannot be scrolled from the carousel. touch-action: pan-y would allow both.
  • Pausing autoplay. A pause button, and stopping autoplay while the mouse or the focus is on the carousel.
  • Keyboard and screen readers. A labelled region, slide labels like “2 of 6”, real buttons for the links and dots, and prefers-reduced-motion.
  • Link order. Next comes before Previous in the markup, so in the order of the keyboard.
  • The size of the dots. It is 8px, and a page can set another with CSS. A CSS variable would be easier, and waits until IE10 is dropped.