Lesson 23 / Components

Mastering Carousels

Time
43 min
Type
Reading + Interactive
Level
Intermediate

Understand carousel structure, Slick setup, fading transitions, and a simple vanilla JavaScript carousel.

Teacher Notes / In-Class Use

Demo Live

  • Use the carousel lab to show that controls, pagination, status text, keyboard support, and motion settings are separate concerns.
  • Show the Slick configuration first, then explain what the hidden teaching JavaScript adds for the interactive lab.

Try In Class

  • Have students identify whether their carousel needs previous and next buttons, dots, autoplay, or none of the above.
  • Ask students to test the carousel with keyboard only before styling it.

Submit Or Check

  • There should be a visible way to move between slides without relying on autoplay.
  • If autoplay exists, students should include a pause strategy and avoid surprising motion.

Watch For

  • All slides being visible because the carousel CSS or initialization did not load.
  • Autoplay treated as decoration while keyboard and status feedback are ignored.

Learning Goals

  • Identify the key parts of a carousel
  • Set up a carousel with Slick
  • Explain how a basic carousel works with vanilla JavaScript
  • Consider responsive and accessible carousel behavior

Interactive Demo

How to use this demo.

Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.

What To Try

  • Toggle controls, dots, visible status, keyboard support, autoplay, and pause behavior.
  • Use the previous and next buttons, dots, and arrow keys to change slides.

What Changes

  • The carousel preview, checklist, and Slick code sample update together.
  • Removing controls or status makes the interaction harder to understand.

What To Notice

  • A carousel is not only slides. It needs controls, state, keyboard support, and motion decisions.
  • Autoplay increases responsibility because users need ways to pause or control motion.

Apply It

  • Check your carousel for visible controls, one active slide, keyboard access, and a pause strategy if autoplay is enabled.

This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source (opens in a new tab).

What are Carousels?

Carousels are dynamic elements that showcase multiple images or content items in a limited space.

They are commonly used on websites to feature product highlights, testimonials, or rotating content.

Key Components of a Carousel

Understanding these foundational elements will help you customize and implement carousels that are functional and user-friendly.

  • Carousel Container: Holds all slides, typically with a fixed width and height.
  • Slides: Individual items displayed, which may contain images, text, or HTML elements.
  • Navigation Controls: Includes previous/next buttons and pagination dots for manual navigation.
  • Autoplay: Optional feature for automatic slide transitions with pause/play controls.
  • Responsive Design: Adapts layout to various screen sizes and devices.
  • Accessibility: Includes keyboard navigation and ARIA attributes for screen readers.

Building a Carousel with Slick

This section demonstrates how to create a carousel using the Slick library, a popular jQuery plugin known for its ease of use and flexibility.

It is suitable for beginners who want a plug-and-play solution.

Visit the Slick documentation for full usage details.

Basic HTML Structure

The following HTML sets up a simple carousel container. You can replace the placeholder content with your actual items.

<div class="your-class">
  <div>your content</div>
  <div>your content</div>
  <div>your content</div>
</div>

Linking Slick and jQuery

Include the necessary CSS and JavaScript files to get Slick working properly.

<link rel="stylesheet" type="text/css" href="slick.css">
<link rel="stylesheet" type="text/css" href="slick-theme.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="slick.min.js"></script>

Implementing Slick with JavaScript

This script initializes the Slick carousel with basic configuration options like navigation dots and smooth transitions.

$(document).ready(function() {
  $('.your-class').slick({
    dots: true,
    infinite: true,
    speed: 500,
    cssEase: 'linear',
  });
});

Creating a Fading Carousel

You can enhance the carousel with a fade transition by adding one line of configuration.

$(document).ready(function() {
  $('.your-class').slick({
    dots: true,
    infinite: true,
    speed: 500,
    fade: true,
    cssEase: 'linear',
  });
});

Building Your Own Carousel with Vanilla JavaScript

This section walks through building a custom carousel without relying on libraries.

It is ideal for learning the underlying mechanics and gaining full control over behavior and design.

<div class="work-slideshow">
  <div class="slide active">Slide 1</div>
  <div class="slide">Slide 2</div>
  <div class="slide">Slide 3</div>
</div>

Vanilla JavaScript Carousel Logic

const carousel = document.querySelector('.work-slideshow');
const slides = carousel.querySelectorAll('.slide');
let currentSlide = 0;

function nextSlide() {
  slides[currentSlide].classList.remove('active');
  currentSlide = (currentSlide + 1) % slides.length;
  slides[currentSlide].classList.add('active');
}

function autoplay() {
  nextSlide();
  setInterval(nextSlide, 3000);
}

autoplay();

Key Considerations

To ensure a polished user experience, be mindful of layout consistency, responsiveness, and accessibility.

  • Use consistent image dimensions to prevent layout issues.
  • Apply media queries for responsive design.
  • Ensure accessibility with keyboard support and alt text for images.

Checkpoint

Before moving on, make sure these feel true.

  • I can explain what controls, dots, status text, and autoplay each do.
  • I can verify only one carousel slide is visually active at a time.
  • I can test carousel navigation without relying on a mouse.

Project Connection

This lesson supports current class projects.

Practice

  • Create a basic carousel structure with three slides.
  • Initialize a carousel with Slick.
  • Build the vanilla JavaScript autoplay example.
  • Add alt text, keyboard support, or pause controls to improve accessibility.

Resources

  • Slick documentation