CSS-first
Responsive in CSS with media queries instead of JavaScript breakpoint objects.
@media (min-width: 768px) {
.aero-slider {
--aero-slides-per-view: 2;
}
} bun add aero-slider
Responsive in CSS with media queries instead of JavaScript breakpoint objects.
@media (min-width: 768px) {
.aero-slider {
--aero-slides-per-view: 2;
}
} The browser does the scrolling. Touch, trackpad and momentum feel native because they are, with CSS scroll snapping holding each slide.
Slides get their size from CSS before any JavaScript runs, so nothing jumps or has to be covered up.
The core is 6.9 KB of JavaScript and 0.9 KB of CSS, gzipped. Looping, autoplay, scrolling dots and thumbnail sync are extensions: ship only the ones you use, or the full file with all of them.
<div id="slider" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Featured">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 5">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 5">Slide 2</div>
<!-- … 5 slides in total -->
</div>
</div>
<div class="aero-slider__pagination" role="group" aria-label="Slides">
<button type="button" class="aero-slider__dot" aria-label="Go to slide {index}"></button>
</div>
</div> #slider {
--aero-slides-per-view: 1;
--aero-slide-gap: 10px;
} import { createSlider, use, scrollingDots } from "aero-slider";
use({ scrollingDots });
const slider = createSlider(document.getElementById("slider"), {
maxDots: 9,
}); 01 Markup
A container, a viewport and a track. Each child of the track is a slide.
Arrows and dots are optional.
You write the roles and labels. The slider keeps state such as aria-current up to date.
<div class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Featured">
<div class="aero-slider__viewport">
<!-- Optional: arrows, placed before the track -->
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 3">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 3">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 3">Slide 3</div>
</div>
</div>
<!-- Optional: pagination, with one dot as a template -->
<div class="aero-slider__pagination" role="group" aria-label="Slides">
<button type="button" class="aero-slider__dot" aria-label="Go to slide {index}"></button>
</div>
</div> 02 Load it
Load it one of three ways:
The CDN and the release include a full file with every extension, which sets up a global AeroSlider. They also include the core and each extension as separate files, so you can load only what you need.
With the package, call use() for each extension you use.
import { createSlider, use, loop } from "aero-slider";
import "aero-slider/slider.css";
use({ loop }); // only the extensions you use <link rel="stylesheet" href="https://unpkg.com/aero-slider/dist/aero-slider.min.css" />
<script type="module" src="https://unpkg.com/aero-slider/dist/aero-slider.min.js"></script> <!-- The files from the release .zip, next to your page -->
<link rel="stylesheet" href="aero-slider.min.css" />
<script type="module" src="aero-slider.min.js"></script> 03 Create it
Pass the container and any options. It returns the slider instance, which is also stored on the element as element.aeroSlider.
const slider = createSlider(document.querySelector(".aero-slider"), { loop: true }); <script type="module">
const slider = AeroSlider.createSlider(document.querySelector(".aero-slider"), { loop: true });
</script> <script type="module">
const slider = AeroSlider.createSlider(document.querySelector(".aero-slider"), { loop: true });
</script> Arrows and dots are not JavaScript options. If the elements are inside the container, the slider finds them and wires them up. The dot is one template that the slider clones for each position it can scroll to.
--aero-slides-per-view, --aero-slide-gap and --aero-slide-aspect, set on the container and changed in media queries.
The library ships layout only. Arrows and dots have no styles, so you style them yourself.
<div id="swatches" role="group" aria-label="Color">
<button type="button" data-variant="graphite">Graphite</button>
<button type="button" data-variant="ceramic">Ceramic</button>
<button type="button" data-variant="blue">Blue</button>
</div>
<div id="variant-gallery" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Product images">
<div class="aero-slider__viewport">
<div class="aero-slider__track">
<!-- data-variant: shown for that color only -->
<div data-variant="graphite">Graphite, front</div>
<div data-variant="graphite">Graphite, side</div>
<div data-variant="ceramic">Ceramic, front</div>
<div data-variant="ceramic">Ceramic, side</div>
<div data-variant="blue">Blue, front</div>
<div data-variant="blue">Blue, side</div>
<!-- No data-variant: shown for every color -->
<div>Fit guide</div>
</div>
</div>
</div>
<!-- One button per slide, in the same order, with the same data-variant -->
<div id="variant-thumbs" class="aero-slider" role="group" aria-label="Product image thumbnails">
<div class="aero-slider__viewport">
<div class="aero-slider__track">
<button type="button" data-variant="graphite" aria-label="Show graphite, front">…</button>
<!-- … -->
<button type="button" aria-label="Show fit guide">…</button>
</div>
</div>
</div> #variant-gallery {
--aero-slide-aspect: 16 / 9;
--aero-slide-gap: 12px;
}
#variant-thumbs {
--aero-slides-per-view: 5;
--aero-slide-gap: 8px;
--aero-slide-aspect: 16 / 9;
} import { createSlider, use, loop, syncThumbnails } from "aero-slider";
use({ loop });
const main = createSlider(document.getElementById("variant-gallery"));
const thumbs = createSlider(document.getElementById("variant-thumbs"), {
alignment: "center",
});
syncThumbnails(main, thumbs);
// Hold on to every slide, in the track or out of it, so each image loads once.
const galleries = [main, thumbs].map((slider) => ({
slider,
track: slider.element.querySelector(".aero-slider__track"),
slides: [...slider.element.querySelectorAll("[data-aero-slider-index]")],
}));
function show(variant) {
for (const { slider, track, slides } of galleries) {
// A slide with no data-variant belongs to every variant.
track.replaceChildren(
...slides.filter((slide) => (slide.dataset.variant ?? variant) === variant)
);
slider.refresh();
}
main.goTo(0, { instant: true }); // leave this out to stay on the same index
}
show("graphite");
document.getElementById("swatches").addEventListener("click", (event) => {
const variant = event.target.closest("button")?.dataset.variant;
if (variant) show(variant);
}); refresh() re-reads the slides from the track, so the slider becomes whatever elements are in it. Change the track first, then call it.refresh() keeps the current index. Call goTo(0, { instant: true }) after it to start the new set from its first image, with no flash.data-aero-slider-index, not by their place in the track, which also holds loop clones.slideChange fires when the set changes under the same index. Update a counter or caption yourself after the swap.<div class="breakout-clip">
<div id="edge-to-edge" class="aero-slider aero-slider--breakout" role="region" aria-roledescription="carousel" aria-label="Featured">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 5">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 5">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 5">Slide 3</div>
<!-- … 5 slides in total -->
</div>
</div>
<div class="aero-slider__pagination" role="group" aria-label="Slides">
<button type="button" class="aero-slider__dot" aria-label="Go to slide {index}"></button>
</div>
</div>
</div> /* Your page's content column: 72rem at most, inside the page gutter. */
:root {
--page-gutter: 1rem;
--content-column: calc(min(72rem, 100vw) - var(--page-gutter) * 2);
}
@media (min-width: 640px) {
:root {
--page-gutter: 1.5rem;
}
}
/* The window's scrollbar would otherwise add a horizontal scroll. */
body {
overflow-x: hidden;
}
/* Full-bleed wrapper: the track spans the whole window. */
.breakout-clip {
width: 100vw;
margin-left: calc(50% - 50vw);
}
/* Slides size against the column, not the window. */
#edge-to-edge {
--aero-slides-per-view: 1.12;
--aero-slide-gap: 12px;
--aero-layout-width: var(--content-column);
}
@media (min-width: 640px) {
#edge-to-edge {
--aero-slides-per-view: 1.35;
--aero-slide-gap: 18px;
}
} import { createSlider } from "aero-slider";
const slider = createSlider(document.getElementById("edge-to-edge"), {
alignment: "start",
}); aero-slider--breakout lets the viewport overflow and pads the track so the first and last slides line up with the column.--aero-layout-width is the width slides are sized against. Set it to your content column.100vw includes the scrollbar.<div id="collections" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Collections">
<div class="aero-slider__viewport">
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 6">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 6">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 6">Slide 3</div>
<!-- … 6 slides in total -->
</div>
</div>
</div> #collections {
--aero-slides-per-view: 1.3;
--aero-slide-gap: 12px;
}
/* From 768px up the slider is destroyed and the same markup is a grid. */
@media (min-width: 768px) {
#collections .aero-slider__track {
display: grid;
grid-template-columns: repeat(3, 1fr);
overflow: visible;
}
} import { createSlider } from "aero-slider";
const el = document.getElementById("collections");
const phone = matchMedia("(max-width: 767px)");
let slider;
function sync() {
if (phone.matches) {
slider ??= createSlider(el);
} else {
slider?.destroy();
slider = undefined;
}
}
phone.addEventListener("change", sync);
sync(); destroy() removes the slider's listeners, clones and state classes and leaves your markup, so the same elements can become a grid and be started again later.<div id="hero-banner" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Featured" data-aero-defer-visibility>
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 4">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 4">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 4">Slide 3</div>
<!-- … 4 slides in total -->
</div>
</div>
<div class="aero-slider__pagination" role="group" aria-label="Slides">
<button type="button" class="aero-slider__dot" aria-label="Go to slide {index}"></button>
</div>
</div> #hero-banner {
--aero-slides-per-view: 1.2;
--aero-slide-aspect: 21 / 9;
--aero-slide-gap: 12px;
} import { createSlider, use, loop, autoplay } from "aero-slider";
use({ loop, autoplay });
const slider = createSlider(document.getElementById("hero-banner"), {
autoplay: true,
autoplayInterval: 4000,
loop: true,
}); prefers-reduced-motion. A manual change restarts the countdown.pause() and resume().data-aero-defer-visibility reserves their space in CSS so slide 1 doesn't jump when JavaScript starts.--aero-slides-per-view and --aero-slide-gap for center alignment. With another alignment, repeat it in the HTML as data-aero-alignment="start" or "end".#ticker {
--aero-slides-per-view: 4;
--aero-slide-gap: 12px;
--aero-slide-aspect: 5 / 2;
} import { createSlider, use, loop, autoplay } from "aero-slider";
use({ loop, autoplay });
const slider = createSlider(document.getElementById("ticker"), {
autoplay: "continuous",
autoplaySpeed: 36,
draggable: false,
}); autoplay: "continuous" drifts the track at autoplaySpeed pixels per second. Like all autoplay it loops, so it needs the loop extension, and it implies snap: "none".pause() do.prefers-reduced-motion the track stays still.draggable: false keeps a mouse press from grabbing the strip. Leave it on if people should scrub through it.<div id="announcement" class="aero-slider aero-slider--vertical" role="region" aria-roledescription="carousel" aria-label="Announcements">
<div class="aero-slider__viewport">
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 3">Free shipping on orders over $50</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 3">Free returns within 30 days</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 3">New: the Autumn field collection</div>
</div>
</div>
</div> /* A vertical slider is as tall as its viewport: one line of text here. */
#announcement .aero-slider__viewport {
height: 2.75rem;
} import { createSlider, use, loop, autoplay } from "aero-slider";
use({ loop, autoplay });
const slider = createSlider(document.getElementById("announcement"), {
vertical: true,
autoplay: true,
autoplayInterval: 3500,
loop: true,
}); vertical: true scrolls top to bottom, whatever the text direction of the page. The up and down arrow keys, the drag axis and a swipe all follow.height, as here, or let it follow --aero-slide-aspect.aero-slider--vertical itself. Putting the class in the markup as well lays the bar out before JavaScript runs.<!-- Main slider -->
<div id="gallery" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Product images">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 5">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 5">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 5">Slide 3</div>
<!-- … 5 slides in total -->
</div>
</div>
</div>
<!-- Thumbnail strip -->
<div id="gallery-thumbs" class="aero-slider" role="group" aria-label="Product image thumbnails">
<div class="aero-slider__viewport">
<div class="aero-slider__track">
<button type="button" aria-label="Show slide 1">Thumbnail 1</button>
<button type="button" aria-label="Show slide 2">Thumbnail 2</button>
<button type="button" aria-label="Show slide 3">Thumbnail 3</button>
<!-- … 5 slides in total -->
</div>
</div>
</div> #gallery {
--aero-slide-aspect: 16 / 9;
--aero-slide-gap: 12px;
}
#gallery-thumbs {
--aero-slides-per-view: 5;
--aero-slide-gap: 8px;
--aero-slide-aspect: 16 / 9;
} import { createSlider, use, loop, syncThumbnails } from "aero-slider";
use({ loop });
const main = createSlider(document.getElementById("gallery"));
const thumbs = createSlider(document.getElementById("gallery-thumbs"), {
alignment: "center",
});
const teardown = syncThumbnails(main, thumbs);
// Before removing the sliders:
// teardown();
// main.destroy();
// thumbs.destroy(); { loop: false } as the third argument to stop that.alignment: "center" on the strip keeps the active thumbnail in the middle.<button type="button"> with its own aria-label, so it can be reached and named.aero-slider__thumb--active and aria-current="true". Style it yourself.destroy().<div id="product-row" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Best sellers">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 8">
<article class="card">
<img src="field-watch.jpg" alt="" />
<a href="/products/field-watch">Field Watch</a>
<button type="button">Add to cart</button>
</article>
</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 8">
<article class="card">
<img src="flight-jacket.jpg" alt="" />
<a href="/products/flight-jacket">Flight Jacket</a>
<button type="button">Add to cart</button>
</article>
</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 8">
<article class="card">
<img src="aviator-glasses.jpg" alt="" />
<a href="/products/aviator-glasses">Aviator Glasses</a>
<button type="button">Add to cart</button>
</article>
</div>
<!-- … 8 slides in total -->
</div>
</div>
</div> /* A peek of the next card on phones, whole rows from there up. */
#product-row {
--aero-slides-per-view: 1.4;
--aero-slide-gap: 12px;
--aero-slide-aspect: auto; /* a card is as tall as its content */
}
@media (min-width: 640px) {
#product-row {
--aero-slides-per-view: 2.4;
}
}
@media (min-width: 1024px) {
#product-row {
--aero-slides-per-view: 4;
--aero-slide-gap: 16px;
}
} import { createSlider } from "aero-slider";
const slider = createSlider(document.getElementById("product-row"), {
alignment: "start",
noDrag: "a, button",
});
// The arrows turn a whole row: as many cards as fully fit.
const el = slider.element;
let perMove = 1;
function pageSize() {
const fit = Math.floor(parseFloat(getComputedStyle(el).getPropertyValue("--aero-slides-per-view")));
if (fit !== perMove) slider.update({ perMove: (perMove = fit) });
}
el.addEventListener("aero:resized", pageSize);
pageSize(); --aero-slides-per-view takes fractions. The part after the point is how much of the next card shows.--aero-slides-per-view whenever the track or a slide changes size along the track, so a media query takes effect with no JavaScript breakpoint config.--aero-slide-aspect: auto lets a card take the height of its content. Leave it out to give every slide one shape.noDrag keeps a mouse drag from starting on the link and button, so they click as usual. Without it, a drag that starts on them swallows the click. Touch is not affected.perMove sets how far the arrows, the arrow keys and autoplay go. Dragging still lands on any card.<div id="max-dots" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Many slides">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 12">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 12">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 12">Slide 3</div>
<!-- … 12 slides in total -->
</div>
</div>
<div class="aero-slider__pagination" role="group" aria-label="Slides">
<button type="button" class="aero-slider__dot" aria-label="Go to slide {index}"></button>
</div>
</div> #max-dots {
--aero-slides-per-view: 2.5;
--aero-slide-gap: 12px;
} import { createSlider, use, scrollingDots } from "aero-slider";
use({ scrollingDots });
const slider = createSlider(document.getElementById("max-dots"), {
maxDots: 5,
}); maxDots, the dots sit in a window that slides to keep the active one in view.aero-slider__dot--edge and aero-slider__dot--near-edge, which the library's CSS shrinks.0, the default, shows every dot.1 / 5
<div id="counted" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Product images">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 5">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 5">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 5">Slide 3</div>
<!-- … 5 slides in total -->
</div>
</div>
</div>
<p id="gallery-count">1 / 5</p>
<div class="progress"><div id="gallery-bar"></div></div> #counted {
--aero-slide-aspect: 16 / 9;
--aero-slide-gap: 12px;
}
.progress {
height: 2px;
background: #2d3036;
}
#gallery-bar {
height: 100%;
background: #6aa5ff;
transition: width 0.25s;
} import { createSlider } from "aero-slider";
const slider = createSlider(document.getElementById("counted"));
const count = document.getElementById("gallery-count");
const bar = document.getElementById("gallery-bar");
function show() {
const position = slider.currentIndex + 1;
count.textContent = `${position} / ${slider.slideCount}`;
bar.style.width = `${(position / slider.slideCount) * 100}%`;
}
slider.element.addEventListener("aero:slideChange", show);
show(); // no slideChange fires for the starting slide aero:slideChange fires on the container as soon as the new slide is known, before the scroll lands, so the counter keeps up with a drag.currentIndex and slideCount from the instance, or the index from event.detail.index.refresh().<div id="styled" class="aero-slider" role="region" aria-roledescription="carousel" aria-label="Styled controls">
<div class="aero-slider__viewport">
<button type="button" class="aero-slider__nav--prev" aria-label="Previous slide">‹</button>
<button type="button" class="aero-slider__nav--next" aria-label="Next slide">›</button>
<div class="aero-slider__track">
<div role="group" aria-roledescription="slide" aria-label="1 of 5">Slide 1</div>
<div role="group" aria-roledescription="slide" aria-label="2 of 5">Slide 2</div>
<div role="group" aria-roledescription="slide" aria-label="3 of 5">Slide 3</div>
<!-- … 5 slides in total -->
</div>
</div>
<div class="aero-slider__pagination" role="group" aria-label="Slides">
<button type="button" class="aero-slider__dot" aria-label="Go to slide {index}"></button>
</div>
</div> #styled {
--aero-slides-per-view: 1.5;
--aero-slide-gap: 12px;
--aero-slide-aspect: 2 / 1;
}
#styled .aero-slider__nav--prev,
#styled .aero-slider__nav--next {
position: absolute;
top: 50%;
z-index: 1;
display: grid;
place-items: center;
width: 40px;
height: 40px;
padding: 0;
border: 1px solid rgb(255 255 255 / 0.16);
border-radius: 50%;
background: rgb(8 9 10 / 0.72);
color: #eceef1;
font-size: 20px;
line-height: 1;
cursor: pointer;
translate: 0 -50%;
transition: border-color 0.15s, opacity 0.15s;
}
#styled .aero-slider__nav--prev {
left: 12px;
}
#styled .aero-slider__nav--next {
right: 12px;
}
#styled .aero-slider__nav--prev:hover,
#styled .aero-slider__nav--next:hover {
border-color: #6aa5ff;
}
/* The slider disables an arrow that cannot go further. */
#styled .aero-slider__nav--prev:disabled,
#styled .aero-slider__nav--next:disabled {
opacity: 0.3;
cursor: default;
border-color: rgb(255 255 255 / 0.16);
}
/* Each dot is a button larger than its mark, so it is easier to tap. */
#styled .aero-slider__pagination {
gap: 0;
}
#styled .aero-slider__dot {
display: grid;
place-items: center;
min-width: 16px;
height: 24px;
padding: 0 2px;
border: 0;
background: none;
}
#styled .aero-slider__dot::before {
content: "";
width: 6px;
height: 6px;
border-radius: 3px;
background: #2d3036;
transition: width 0.2s, background-color 0.2s;
}
/* The slider marks the current position's dot. */
#styled .aero-slider__dot--active::before {
width: 20px;
background: #6aa5ff;
} import { createSlider } from "aero-slider";
const slider = createSlider(document.getElementById("styled")); aria-label. Make the dot a <button type="button"> too: a <span> cannot be reached by keyboard. {index} in its aria-label becomes the slide number of each dot.position: relative, so arrows inside it can be placed with position: absolute.disabled on an arrow that cannot go further, so :disabled styles the end state. With loop no arrow is ever disabled.aero-slider__dot--active and aria-current="true".::before, as here.Function
Turns a container into a slider and returns its instance, also stored as
element.aeroSlider. config takes any of the
options below.
Arrows and dots are not options. Put them in the markup and the slider wires them up; see arrows and dots.
import { createSlider, use, loop } from "aero-slider";
use({ loop }); // the loop option needs the loop extension
const el = document.querySelector(".aero-slider");
const slider = createSlider(el, { loop: true });
el.aeroSlider === slider; // true aero-slider: missing .aero-slider__track No track element inside the container. aero-slider: no slides found The track has no children. Config
The second argument to createSlider. Every option is optional. Change them later
with slider.update().
start is the left edge, the right edge in a right-to-left slider and the top in a vertical one. Without loop, the scroll stops at the ends, but currentIndex still names the active slide.dir="rtl". Call refresh() after changing dir.next(), prev(), the arrows, the arrow keys and autoplay move at once."input, textarea, select, [contenteditable]", keeps form fields working. Your value replaces it. Links and buttons are left out so a slide that is a link can still be dragged."native" uses CSS scroll snapping. "settle" scrolls freely, then eases onto the nearest slide. "none" stops wherever the scroll ends.next(), prev() and goTo() take to scroll, eased by scrollEasing. 0 uses native smooth scrolling. Instant under prefers-reduced-motion.scrollDuration scrolls. (t) => t is linear.CSS
Size and spacing live in CSS, set on the container. The slider re-measures on its own when
the container or a slide changes size, including from media queries and loaded images. After
you change a variable from script, call refresh().
1 or more. Fractions such as 1.5 show a peek of the next slide.aero-slider--breakout slider. Slides size to this column while the track runs to the edges of the screen.alignment. Write it in your HTML too when you use a non-center alignment, so snapping and the reserved space match before JavaScript runs.Bild {index} von {count}.
The slider also adds state classes such as aero-slider--dragging to the container.
They are listed under States.
CSS
Classes the slider sets on the container and its controls. See arrows, dots and their states for an example.
draggable is on. The track shows the grab cursor only then.grabbing cursor).snap is "settle" or "none". Turns CSS scroll snapping off.vertical is on.aero-slider__nav--prev or --next when it cannot go further. Never with loop.Instance
createSlider returns an instance with methods to move, pause, change and tear down
the slider.
perMove stops. Stops at the last slide unless loop is on.perMove stops. Stops at the first slide unless loop is on.index. Out-of-range indexes wrap on a loop and clamp otherwise. { instant: true } skips the animation. Use it instead of scrolling the track yourself.aero:destroy, then removes its listeners, clones and classes and deletes element.aeroSlider. Your markup stays, so createSlider can start it again.
The instance stays on element.aeroSlider until destroy().
DOM events
The slider fires bubbling CustomEvents on its container, named with the
aero: prefix, with data on event.detail. In the recipes,
el is the container.
el.addEventListener("aero:slideChange", (event) => {
console.log(event.detail.index);
}); Use it for A “2 / 5” counter, analytics, or keeping your own UI in step with the slider.
const counter = document.querySelector(".slide-counter");
const slider = createSlider(el, { loop: true });
const show = (index) => {
counter.textContent = `${index + 1} / ${slider.slideCount}`;
};
show(slider.currentIndex); // no slideChange for the starting slide
el.addEventListener("aero:slideChange", (event) => show(event.detail.index)); Use it for Lazy-loading an image or starting a video when its slide comes into view.
// Each slide holds <img data-src="photo.jpg" alt="">.
el.addEventListener("aero:visible", (event) => {
const track = el.querySelector(".aero-slider__track");
// Real slides carry data-aero-slider-index; loop clones don't.
const slide = track.querySelector(`[data-aero-slider-index="${event.detail.index}"]`);
const src = slide.querySelector("img[data-src]")?.dataset.src;
if (!src) return;
// Clones are copies of the slide, so load every copy of the image.
for (const img of track.querySelectorAll("img[data-src]")) {
if (img.dataset.src !== src) continue;
img.src = src;
img.removeAttribute("data-src");
}
});
createSlider(el, { loop: true }); Use it for Pausing a video, or any other work, when its slide leaves the view.
// Some slides hold <video muted playsinline loop>.
// A loop comes to rest on real slides, so the real copy is the one in view.
const videoIn = (index) =>
el.querySelector(`[data-aero-slider-index="${index}"] video`);
el.addEventListener("aero:visible", (event) => videoIn(event.detail.index)?.play());
el.addEventListener("aero:hidden", (event) => videoIn(event.detail.index)?.pause()); index is the slide it started from. Touch swipes fire no drag events; use aero:slideChange to follow every move.Use it for Reacting to the first drag, such as removing a “drag to explore” hint.
const hint = el.querySelector(".drag-hint");
el.addEventListener("aero:dragStart", () => hint.remove(), { once: true }); fromIndex is where it started, index where it is going. Mouse and pen only, like aero:dragStart.Use it for Logging drags. You don't need it to block clicks: the slider already swallows the click that ends a drag.
el.addEventListener("aero:dragEnd", (event) => {
const { fromIndex, index } = event.detail;
if (index !== fromIndex) console.log(`dragged from ${fromIndex} to ${index}`);
}); Use it for Hiding anything pinned to where a slide was, before the slider moves it.
// A tooltip pinned to a slide would point at the wrong spot once slides move.
const tip = document.querySelector(".slide-tip");
el.addEventListener("aero:resize", () => {
tip.hidden = true;
}); Use it for Recomputing anything that depends on slide size or position.
// Hand the slide width to CSS, e.g. to size captions outside the track.
const setSlideWidth = () => {
const slide = el.querySelector("[data-aero-slider-index]");
el.style.setProperty("--slide-width", `${slide.offsetWidth}px`);
};
el.addEventListener("aero:ready", setSlideWidth);
el.addEventListener("aero:resized", setSlideWidth);
createSlider(el); createSlider finished setting up. Fires before createSlider returns, so listen first, or check el.aeroSlider later. The container also gets aero-slider--ready.Use it for Setup that needs the instance when other code creates the slider, such as a framework component or a CMS block.
// Works whether this runs before or after createSlider.
function setUp(slider) {
const counter = slider.element.querySelector(".slide-counter");
if (counter) counter.textContent = `${slider.currentIndex + 1} / ${slider.slideCount}`;
}
if (el.aeroSlider) setUp(el.aeroSlider);
else el.addEventListener("aero:ready", () => setUp(el.aeroSlider), { once: true }); destroy() was called. It fires first, while the instance still works, and teardown follows.Use it for Removing your own listeners and timers that belong to this slider.
const slider = createSlider(el);
const listeners = new AbortController();
// Your own listeners for this slider share one signal…
document.addEventListener("keydown", (event) => event.key === "n" && slider.next(), {
signal: listeners.signal,
});
// …so they all go when it does.
el.addEventListener("aero:destroy", () => listeners.abort(), { once: true }); Events bubble, so a listener on document hears every slider on the page. event.target is the container and event.target.aeroSlider its instance. If sliders nest, check event.target === el.
addEventListener types these events as a plain Event. Cast to CustomEvent with the exported SliderEventData to type detail.
import type { SliderEventData } from "aero-slider";
type DragEnd = CustomEvent<SliderEventData<"dragEnd">>;
el.addEventListener("aero:dragEnd", (event) => {
const { detail } = event as DragEnd;
console.log(detail.fromIndex, detail.index);
}); Overview
The core, 7.8 KB gzipped, has slides, arrows, dots, the keyboard, drag, snapping and every event. Looping, autoplay, scrolling dots and thumbnail sync are extensions. The full file, 10.7 KB, has all of them, ready to use.
An option whose extension isn't loaded does nothing, and the console says which extension it needs.
Autoplay requires the loop extension, because autoplay always loops.
Without a bundler, load the core file and then each extension file after it, or join them into one file in that order.
import { createSlider, use, loop, autoplay } from "aero-slider";
// The core's CSS and loop's. "aero-slider/slider.css" has every extension's.
import "aero-slider/core.css";
import "aero-slider/extensions/loop.css";
// Once, before creating sliders. Extensions you never import
// are left out of your bundle.
use({ loop, autoplay });
createSlider(el, { autoplay: true }); <link rel="stylesheet" href="https://unpkg.com/aero-slider/dist/aero-slider.core.min.css" />
<link rel="stylesheet" href="https://unpkg.com/aero-slider/dist/extensions/aero-slider.loop.min.css" />
<script type="module" src="https://unpkg.com/aero-slider/dist/aero-slider.core.min.js"></script>
<script type="module" src="https://unpkg.com/aero-slider/dist/extensions/aero-slider.loop.min.js"></script>
<script type="module" src="https://unpkg.com/aero-slider/dist/extensions/aero-slider.autoplay.min.js"></script> # Core first, then each extension, in the order the table lists them.
cat aero-slider.core.min.js extensions/aero-slider.loop.min.js extensions/aero-slider.autoplay.min.js > slider.js
cat aero-slider.core.min.css extensions/aero-slider.loop.min.css > slider.css | Extension | Adds | Requires | Files in dist/extensions | Gzipped |
|---|---|---|---|---|
| loop | loop | core | aero-slider.loop.min.js, aero-slider.loop.min.css | 1.2 KB |
| autoplay | autoplay, interval and continuous | core + loop | aero-slider.autoplay.min.js | 1.4 KB |
| scrollingDots | maxDots | core | aero-slider.scrolling-dots.min.js, aero-slider.scrolling-dots.min.css | 1.0 KB |
| thumbnails | syncThumbnails() | core, and loop for a looping strip | aero-slider.thumbnails.min.js | 1.2 KB |
Extension
dist/extensions/aero-slider.loop.min.jsdist/extensions/aero-slider.loop.min.css
Wraps from the last slide back to the first, using clones of the slides on both sides of the track. Clones are hidden from screen readers, the tab order and forms.
Cloned custom elements start up when inserted, so measure them in a
ResizeObserver, not in the constructor.
#ext-loop {
--aero-slides-per-view: 2.5;
--aero-slide-gap: 12px;
} import { createSlider, use, loop } from "aero-slider";
use({ loop });
const slider = createSlider(document.getElementById("ext-loop"), {
loop: true,
});
// The switch above the slider:
slider.update({ loop: false }); Extension
dist/extensions/aero-slider.autoplay.min.js
Moves the slider on its own: one move every interval, or a continuous drift like a ticker. It always loops, so it needs the loop extension too. Without it, autoplay stops at the last slide and the console says so.
Autoplay pauses while the slider is pressed, dragged or has keyboard focus, while the tab is
hidden, and on pause(). Interval autoplay also pauses on hover. Neither kind runs
under reduced motion.
#ext-autoplay {
--aero-slides-per-view: 3;
--aero-slide-gap: 12px;
} import { createSlider, use, loop, autoplay } from "aero-slider";
use({ loop, autoplay });
const slider = createSlider(document.getElementById("ext-autoplay"), {
autoplay: true,
autoplayInterval: 2500,
autoplaySpeed: 48,
});
// The mode switch above the slider:
slider.update({ autoplay: "continuous" }); // drift at autoplaySpeed
slider.update({ autoplay: true }); // one move every autoplayInterval
// The pause switch. Autoplay that runs for more than five seconds needs one.
slider.pause();
slider.resume(); true moves once every autoplayInterval. "continuous" drifts like a ticker at autoplaySpeed, with snap: "none". Off under prefers-reduced-motion."continuous" autoplay in pixels per second. 0 or less keeps the track still.resume(). Hover, focus and drag pauses still apply on top.pause() and starts a fresh autoplay countdown.prefers-reduced-motion. The first one fires inside createSlider, so listen before you create.Use it for A progress bar or a playing light that matches what autoplay is really doing.
// <div class="autoplay-bar"> with transform-origin: left, inside the container.
const interval = 4000;
const bar = el.querySelector(".autoplay-bar");
let fill = null;
const restart = () => {
fill?.cancel();
fill = bar.animate([{ transform: "scaleX(0)" }, { transform: "scaleX(1)" }], {
duration: interval,
});
};
el.addEventListener("aero:autoplayStart", restart);
// Every slide change restarts the countdown, so the bar starts over too.
el.addEventListener("aero:slideChange", () => fill && restart());
el.addEventListener("aero:autoplayStop", () => {
fill?.cancel();
fill = null;
});
createSlider(el, { autoplay: true, autoplayInterval: interval, loop: true }); pause(), a hidden tab, update() turning it off, or destroy(). Use it for Taking that progress bar or light down again. Pair it with aero:autoplayStart.
// Style .is-playing as an indicator light that is on while autoplay runs.
el.addEventListener("aero:autoplayStart", () => el.classList.add("is-playing"));
el.addEventListener("aero:autoplayStop", () => el.classList.remove("is-playing"));
createSlider(el, { autoplay: true, loop: true }); Extension
dist/extensions/aero-slider.scrolling-dots.min.jsdist/extensions/aero-slider.scrolling-dots.min.css
Shows at most maxDots pagination dots and scrolls the rest into view as the slider
moves, the way iOS page indicators do. Plain dots are in the core; this extension adds the
scrolling. See the example with many slides.
#ext-scrolling-dots {
--aero-slides-per-view: 2.5;
--aero-slide-gap: 12px;
} import { createSlider, use, scrollingDots } from "aero-slider";
use({ scrollingDots });
const slider = createSlider(document.getElementById("ext-scrolling-dots"), {
maxDots: 5,
});
// The switch above the slider:
slider.update({ maxDots: 0 }); // every dot 0 shows them all.maxDots: the outermost dot of the window when more dots lie beyond it. aero-slider__dot--near-edge marks the next one in, and aero-slider__dot--hidden the dots outside the window.Extension
dist/extensions/aero-slider.thumbnails.min.js
Links a thumbnail strip to a main slider: clicking a thumbnail shows that slide, and the
strip follows the main slider. Both are made with createSlider. See the
gallery example.
#ext-main {
--aero-slides-per-view: 1.3;
--aero-slide-aspect: 16 / 9;
--aero-slide-gap: 12px;
}
#ext-thumbs {
--aero-slides-per-view: 5;
--aero-slide-gap: 8px;
--aero-slide-aspect: 16 / 9;
} import { createSlider, use, loop, syncThumbnails } from "aero-slider";
use({ loop });
const main = createSlider(document.getElementById("ext-main"));
const thumbs = createSlider(document.getElementById("ext-thumbs"), {
alignment: "center",
});
let teardown = syncThumbnails(main, thumbs);
// The switch above the gallery:
teardown();
teardown = syncThumbnails(main, thumbs, { loop: false }); primary.loop option. The strip can still be dragged.primary.aero-slider__thumb--active and aria-current="true", and scrolls the strip to keep it in view.role="button" and tabindex="0", so the keyboard can reach them. One with no label gets aria-label="Go to slide N".alignment: "center" on the strip to keep the active thumbnail centered.syncThumbnails on the thumbnail of the current slide.Chrome 88+, Firefox 89+ and Safari 16+.
| CSS Feature | Chrome | Safari | Firefox | Used For |
|---|---|---|---|---|
| CSS Variables | 49+ | 9.1+ | 31+ | Configuring layout and gap size. |
| Scroll Snapping | 69+ | 11+ | 68+ | Locking slides into place after scrolling. |
| aspect-ratio | 88+ | 15+ | 89+ | Sizing slides from --aero-slide-aspect. |
| contain: paint | 52+ | 15.4+ | 69+ | Keeping slide painting inside the viewport. |
| overscroll-behavior-x | 63+ | 16+ | 59+ | Preventing rubber-banding when hitting the edge. |