Carousel
Carousel is a flexible content slider for image galleries, hero banners, product showcases, onboarding flows, testimonials, and any interface that needs to cycle through multiple items inside one visual area. The component supports slide and fade modes, pointer-based swipe gestures, autoplay, controls, pager dots, thumbnails, looping, and direct JavaScript control.
Getting Started
Create a .carousel root, place every slide inside .carousel-items, and use .item for each individual slide.
Use class in HTML examples and className in React examples.
import { Carousel } from 'frontalign';
const carousel = Carousel.create('#featured-carousel', {
mode: 'slide',
loop: true,
controls: true,
pager: true,
swipe: true,
autoplay: {
enabled: true,
interval: 4000,
pauseOnHover: true,
pauseOnSwipe: true,
},
});
Structure
The carousel structure is intentionally predictable: one root, one list wrapper, and one .item per slide.
Controls, pager dots, and thumbnails are injected by the runtime when their options are enabled.
| Element | Class / Attribute | Purpose |
|---|---|---|
| Root | carousel | Main carousel container. The JavaScript instance is attached to this element. |
| Items wrapper | carousel-items | Holds the slide list and receives transform or fade behavior. |
| Slide item | item | Represents one slide. Each item may contain images, cards, text, buttons, or custom HTML. |
| Active slide | is-active | Applied to the currently visible slide, especially important in fade mode. |
| Fade mode marker | is-fade | Added to the root when mode: "fade" is enabled. |
| Controls | carousel-control | Previous and next buttons injected by the runtime when controls are enabled. |
| Pager | pager | Dot navigation injected after the items container when pager is enabled. |
| Thumbnails | thumbnails | Thumbnail navigation strip. When enabled, it takes priority over the pager. |
Configuration Options
Carousel options control the transition mode, navigation UI, swipe support, autoplay behavior, and thumbnail rendering.
| Option | Type | Default | Description |
|---|---|---|---|
mode | string | "slide" | Transition style. Use "slide" for horizontal movement or "fade" for opacity transitions. |
loop | boolean | true | Wraps from the last slide to the first and from the first slide back to the last. |
controls | boolean | true | Renders previous and next arrow controls. |
pager | boolean | true | Renders dot-based navigation below the slides. |
swipe | boolean | true | Enables pointer-based swipe gestures for touch and mouse input. |
autoplay.enabled | boolean | true | Automatically advances the carousel. |
autoplay.interval | number | 4000 | Delay in milliseconds between automatic transitions. |
autoplay.pauseOnHover | boolean | true | Pauses autoplay while the pointer is over the carousel. |
autoplay.pauseOnSwipe | boolean | true | Pauses autoplay while the user is actively swiping. |
thumbnails.enabled | boolean | false | Renders a thumbnail strip instead of dot navigation. |
thumbnails.clickable | boolean | true | Allows thumbnails to navigate directly to their matching slide. |
Fade Mode
Fade mode stacks slides and transitions by opacity. It is ideal for hero banners, marketing sections, and calm background image rotations.
When fade mode is enabled, the runtime adds .is-fade to the root and toggles .is-active on the visible .item.
{
mode: 'fade',
autoplay: {
enabled: true,
interval: 5000,
pauseOnHover: true
}
}
Thumbnail Navigation
Thumbnails render a .thumbnails strip. Each thumbnail is generated from the <img> element inside the matching slide.
Use this mode for product galleries, portfolios, and image-heavy interfaces where preview navigation is useful.
<div class="carousel" id="thumbnail-carousel">
<ul class="carousel-items">
<li class="item">
<img src="/images/gallery-1.jpg" alt="Gallery item one" />
</li>
<li class="item">
<img src="/images/gallery-2.jpg" alt="Gallery item two" />
</li>
<li class="item">
<img src="/images/gallery-3.jpg" alt="Gallery item three" />
</li>
</ul>
</div>
{
pager: false,
thumbnails: {
enabled: true,
clickable: true
}
};
Swipe Support
Carousel uses the native Pointer Events API for cross-device swipe detection. It supports touch screens, trackpads, and mouse drag interactions. A swipe is recognized when horizontal movement passes the internal threshold, the gesture is long enough, and horizontal movement is dominant over vertical movement.
{
swipe: true,
autoplay: {
pauseOnSwipe: true
}
};
| Gesture rule | Default behavior |
|---|---|
dx threshold | Horizontal displacement must exceed the internal swipe threshold. |
120ms duration guard | Very short accidental gestures are ignored. |
| Horizontal dominance | Vertical page scrolling is protected from accidental carousel navigation. |
Autoplay
Autoplay advances the carousel at a configured interval and pauses during user interaction when enabled. When users manually navigate with controls, pager dots, or thumbnails, autoplay restarts from the current interval so the rhythm stays consistent.
{
autoplay: {
enabled: true,
interval: 5000,
pauseOnHover: true,
pauseOnSwipe: true
}
};
Instance Methods
Direct instances expose lifecycle methods for initialization and cleanup.
| Method | Purpose |
|---|---|
build() | Initializes the carousel, injects enabled controls, pager or thumbnails, binds events, enables swipe, and starts autoplay. |
dispose() | Stops autoplay, removes event listeners, removes injected UI, and unregisters the instance from Carousel.instances. |
const carousel = new Carousel('#featured-carousel', options);
carousel.build();
// Later, when the component or page is destroyed
carousel.dispose();
Static Instance Management
Every active carousel is stored in Carousel.instances, a static Map that lets you inspect or control instances globally.
import { Carousel } from 'frontalign';
const allCarousels = Carousel.instances;
Carousel.instances.forEach((instance, id) => {
console.log(id, instance);
});
Use this for debugging, admin previews, documentation sandboxes, or global teardown logic.
Accessibility
Carousel content should remain understandable for keyboard, screen reader, and reduced-motion users. Keep slide content semantic and always provide meaningful alt text for images.
| Practice | Reason |
|---|---|
Use descriptive alt text | Image-only slides need accessible labels so the content is not lost. |
| Keep controls visible | Users should not rely only on autoplay or swipe to navigate. |
| Avoid fast autoplay | Short intervals can make content difficult to read and interact with. |
| Dispose on unmount | Prevents duplicate listeners and unexpected behavior in SPAs. |
| Do not hide important content only inside slides | Critical information should remain reachable even if JavaScript fails. |
Class Reference
Use this reference when building custom carousel markup, debugging runtime output, or styling advanced carousel layouts.
| Class | Applied to | Description |
|---|---|---|
carousel | Root | Base carousel container. |
carousel-items | Items wrapper | Slide track or fade stack container. |
item | Slide | Individual carousel slide. |
is-active | Slide / pager / thumbnail | Marks the current visible or selected item. |
is-fade | Root | Enables fade-mode styling on the carousel root. |
carousel-control | Injected button | Base class for previous and next controls. |
pager | Injected list | Dot navigation wrapper. |
thumbnails | Injected list | Thumbnail navigation wrapper. |
Best Practices
Use slide mode for galleries and structured cards. Use fade mode for hero banners and visual storytelling.
Keep autoplay slow enough for real reading, keep navigation controls available, and clean up instances in SPA environments with .dispose().
When thumbnails are enabled, disable pager intentionally so the navigation pattern stays clear and predictable.



