Skip to content

Carousel

The Carousel component provides a versatile and customizable slideshow for cycling through elements like images or slides of content. It supports various transition effects, navigation controls, and automatic cycling.

<div class="carousel">
<div class="slides">
<div class="slide">Slide 1</div>
<div class="slide">Slide 2</div>
<div class="slide">Slide 3</div>
</div>
</div>
<div class="carousel" data-role="carousel">
<div class="slides">
<div class="slide" data-cover="image1.jpg"></div>
<div class="slide" data-cover="image2.jpg"></div>
<div class="slide" data-cover="image3.jpg"></div>
</div>
</div>
<div class="carousel"
data-role="carousel"
data-auto-start="true"
data-effect="fade"
data-control-prev="<span class='mif-arrow-left'></span>"
data-control-next="<span class='mif-arrow-right'></span>">
<div class="slides">
<div class="slide">Slide 1</div>
<div class="slide">Slide 2</div>
<div class="slide">Slide 3</div>
</div>
</div>

The Carousel component accepts the following configuration options:

ParameterTypeDefaultDescription
autoStartbooleanfalseWhether to automatically start the carousel
widthstring"100%"Width of the carousel
heightstring"16/9"Height of the carousel (can be aspect ratio like “16/9”, “4/3”, “21/9” or a fixed pixel value)
effectstring"slide"Transition effect between slides. Options: “slide”, “slide-v”, “fade”, “switch”, “zoom”, “swirl”
effectFuncstring"linear"Easing function for the transition effect
directionstring"left"Direction of the carousel movement. Options: “left”, “right”
durationnumber300Duration of the transition effect in milliseconds
periodnumber5000Time between slide transitions in milliseconds
stopOnMousebooleantrueWhether to pause the carousel when the mouse hovers over it
controlsbooleantrueWhether to show the next/previous controls
controlsOnMousebooleanfalseWhether to show controls only when the mouse hovers over the carousel
controlsOutsidebooleanfalseWhether to position controls outside the carousel
controlPrevstring"&larr;"HTML content for the previous control
controlNextstring"&rarr;"HTML content for the next control
bulletsbooleantrueWhether to show the bullet navigation
bulletsStylestring"square"Style of the bullets. Options: “square”, “circle”, “rect”, “diamond”
bulletsSizestring"default"Size of the bullets. Options: “default”, “mini”, “small”, “large”
bulletsPositionstring"default"Position of the bullets. Options: “default”, “left”, “right”
ParameterTypeDefaultDescription
clsCarouselstring""Additional CSS class for the carousel element
clsSlidesstring""Additional CSS class for the slides container
clsSlidestring""Additional CSS class for each slide
clsControlsstring""Additional CSS class for the controls
clsControlNextstring""Additional CSS class for the next control
clsControlPrevstring""Additional CSS class for the previous control
clsBulletsstring""Additional CSS class for the bullets container
clsBulletstring""Additional CSS class for each bullet
clsBulletOnstring""Additional CSS class for the active bullet
clsThumbOnstring""Additional CSS class for the active thumbnail
CallbackDescription
onStopTriggered when the carousel stops
onStartTriggered when the carousel starts
onPlayTriggered when the carousel plays
onSlideClickTriggered when a slide is clicked
onBulletClickTriggered when a bullet is clicked
onThumbClickTriggered when a thumbnail is clicked
onMouseEnterTriggered when the mouse enters the carousel
onMouseLeaveTriggered when the mouse leaves the carousel
onNextClickTriggered when the next control is clicked
onPrevClickTriggered when the previous control is clicked
onSlideShowTriggered when a slide is shown
onSlideHideTriggered when a slide is hidden
onCarouselCreateTriggered when the carousel is created

The Carousel component provides the following API methods:

  • toSlide(index) - Navigate to a specific slide by index
  • next() - Navigate to the next slide
  • prev() - Navigate to the previous slide
  • stop() - Stop the carousel
  • play() - Start the carousel
  • setEffect(effect) - Change the transition effect
// Get the carousel element
const carousel = Metro.getPlugin('#myCarousel', 'carousel');
// Navigate to a specific slide
carousel.toSlide(2);
// Navigate to the next slide
carousel.next();
// Stop the carousel
carousel.stop();
// Start the carousel
carousel.play();
// Change the transition effect
carousel.setEffect('fade');

The Carousel component can be styled using the following CSS variables:

VariableDefault (Light)Dark ModeDescription
--carousel-backgroundvar(--default-background)var(--default-background)Background color of the carousel
--carousel-switch-colorvar(--default-color)var(--default-color)Color of the navigation switches
--carousel-switch-color-hovervar(--default-color-hover)var(--default-color-hover)Hover color for navigation switches
--carousel-bullet-color#f5f8fe#26282eColor of the bullets
--carousel-bullet-color-active#50a8ff#50a8ffColor of the active bullet
/* Custom styling for a specific carousel */
#myCustomCarousel {
--carousel-background: #f0f8ff;
--carousel-switch-color: #0078d7;
--carousel-switch-color-hover: #0063b1;
--carousel-bullet-color: #d0d0d0;
--carousel-bullet-color-active: #0078d7;
}
  • .carousel - Main carousel container
  • .slides - Container for slides
  • .slide - Individual slide
  • .active-slide - Currently active slide
  • .carousel-switch-next - Next control button
  • .carousel-switch-prev - Previous control button
  • .carousel-bullets - Container for bullet navigation
  • .carousel-bullet - Individual bullet
  • .bullet-on - Active bullet
  • .default-size - Default size bullets
  • .mini-size - Mini size bullets
  • .small-size - Small size bullets
  • .large-size - Large size bullets
  • .bullet-style-square - Square bullets
  • .bullet-style-cycle - Circular bullets
  • .bullet-style-diamond - Diamond-shaped bullets
  • .bullet-style-rect - Rectangular bullets
  • .controls-outside - Places controls outside the carousel

The Carousel component includes features for improved accessibility:

  • Proper keyboard navigation support
  • ARIA attributes for screen readers
  • Pause on hover functionality to give users time to read content
  • High contrast visual indicators
  1. Always provide alternative text for image slides to improve accessibility
  2. Use appropriate slide transition durations (not too fast or too slow)
  3. Consider disabling auto-rotation for content that requires more time to read
  4. Ensure sufficient contrast between slide content and background
  5. Test carousel navigation with keyboard-only users in mind
  6. Use meaningful content in slides rather than purely decorative elements