carousel-wc

Scroll carousel with prev/next buttons, dot indicators, autoplay, keyboard navigation, and ARIA.

Overview

A scroll-snap carousel with full progressive enhancement. Without JavaScript it renders as a simple scrollable row. With JS it adds prev/next buttons, dot indicators, autoplay, looping, keyboard navigation, and ARIA roles.

Usage

Wrap slide elements inside <carousel-wc>. Each direct child becomes a slide.

Autoplay with Looping

Add autoplay and loop to cycle slides automatically. Autoplay pauses on hover and focus, and is disabled entirely when prefers-reduced-motion is active.

Multi-item Carousel

Set item-width="auto" to show multiple items at once with a gap.

No Indicators

Set indicators="false" to hide dot indicators.

Attributes

AttributeValuesDefaultDescription
autoplaybooleanAuto-advance slides
autoplay-delaynumberDelay between auto-advance in milliseconds
loopbooleanLoop back to start after last slide
indicatorsstringShow slide position indicators
item-widthstringCSS width for each slide item
gap"xs", "s", "m", "l", "xl"Gap between slides
startnumberInitial slide index
persiststringlocalStorage key for position persistence
transition"fade", "slide", "scale"View Transition animation between slides

Required Structure

ElementRequiredDescription
any child elementsyesEach direct child becomes a slide

Events

JavaScript API

Property / MethodTypeDescription
element.currentIndexnumberCurrent slide index (read-only)
element.slideCountnumberTotal number of slides (read-only)
element.playingbooleanWhether autoplay is active (read-only)
element.next()methodGo to next slide
element.prev()methodGo to previous slide
element.goTo(index)methodJump to specific slide
element.play()methodStart autoplay
element.pause()methodPause autoplay
element.reset()methodReturn to initial slide, clear persistence

Progressive Enhancement

Without JavaScript, <carousel-wc> renders as a horizontal scrollable row using the :not(:defined) fallback. Once JS registers the component, controls and indicators appear.

View Transitions

Add transition to switch from scroll-snap to View Transition animations. When active, the carousel uses a stacked-grid layout and animates between slides using the View Transitions API.

ValueEffect
fade (default)Crossfade between slides
slideDirectional slide — forward when advancing, backward when retreating
scaleScale down old slide, scale up new slide

Dual-Mode Behavior

Without transition, the carousel uses scroll-snap with IntersectionObserver — slides are arranged in a horizontal row and the user scrolls between them. With transition, the carousel switches to a stacked CSS Grid layout where slides occupy the same grid cell and are shown/hidden with View Transition animations.

Related