Skip to the page

Classic case studies · mid

Case study: image carousel and gallery

A strong carousel design has a controlled/uncontrolled index API, moves slides with CSS transforms or scroll-snap, loads the current image eagerly and only neighbours next, supports swipe without breaking vertical scroll, and follows the APG carousel pattern with a pause control.

The notes

What you must know

  • API: items, index + onIndexChange (controlled) or defaultIndex, loop, autoplay interval, renderItem, thumbnails, aria label.
  • Movement: transform: translateX(-index × 100%) (compositor-friendly) or CSS scroll-snap (native swipe and momentum); never animate left/width.
  • Loading: current slide eager (fetchpriority high if it is the LCP image), preload neighbours, lazy-load the rest; srcset/sizes; reserve aspect ratio.
  • Infinite loop: clone first/last slides and jump without a transition, or wrap the index with modulo ((i % n) + n) % n.
  • Swipe: pointer events with a distance/velocity threshold; touch-action: pan-y so vertical page scroll still works.
  • APG carousel: container role=region (or group) with aria-roledescription='carousel' and a label; slides role=group, aria-roledescription='slide'; a Stop/Start rotation button as the first control; stop rotation on hover and on keyboard focus; aria-live='off' while rotating, 'polite' when not.
  • Respect prefers-reduced-motion (no autoplay, no slide animation); pause autoplay when the tab is hidden or the carousel is off-screen.
  • Gallery/lightbox variant: thumbnail strip, zoom in a modal dialog, arrow keys, the photo index in the URL for sharing.

The red pen

Common traps: mistake → fix

  • Mistake: Autoplay without a pause button

    Fix: fails WCAG and annoys users.

  • Mistake: Load all 50 high-resolution images up front

    Fix: slow LCP and wasted data.

  • Mistake: i - 1 for 'previous' at index 0

    Fix: negative index; JavaScript % keeps the sign.

  • Mistake: Animate left

    Fix: layout on every frame and jank.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design a product image carousel with thumbnails and swipe.
  2. There are 50 high-res images. What loads and when?
  3. How do you make autoplay accessible?
  4. How do you build an infinite loop without a visible jump?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview