跪拜 Guibai
← All articles
Vue.js

Making an Auto-Playing Carousel Actually Pauseable: A Vue Component's WCAG Overhaul

By ruanno_top ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Auto-playing carousels are common on marketing sites, and most of them fail WCAG 2.2.2 by lacking a pause, stop, or hide mechanism. The explicit pause button—placed first in the tab order—covers all input methods, unlike hover or focus-based shortcuts that break for keyboard and touch users.

Summary

The 0.2.26 release of a Vue carousel turns autoplay into an accessibility problem instead of an animation parameter. The original auto-advancing carousel had zero pause controls, no aria-live, no aria-roledescription, and kept ticking even under prefers-reduced-motion. Measuring with a 1-second interval and 14 assertions revealed that the first Tab stop landed on the previous arrow, focus didn't stop the timer, and screen readers got no announcement when slides changed.

The fix adds a real pause button placed before the arrows in DOM order, so the first Tab lands on it. The button is visually hidden with opacity:0 until hover, keyboard focus, or paused state, and uses aria-pressed. aria-live switches between off during autoplay and polite when paused, so screen readers aren't interrupted every three seconds. The carousel region gets role="region", aria-roledescription="carousel", and aria-label, and indicators get aria-current.

prefers-reduced-motion is read via matchMedia on mount, defaulting to paused, and visibilitychange clears the timer when the tab is hidden. The result: 14/14 real-device assertions pass, unit tests grew from 923 to 926, and the updated demo shows a working pause button.

Takeaways
— The original carousel had no pause control; setInterval ran from mount to unmount, with only a temporary stop while dragging.
— First Tab stop changed from the previous arrow to the pause button in 0.2.26 by placing the button before arrows and dots in DOM order.
— aria-live must be off during autoplay and polite when paused to avoid interrupting screen readers every 3 seconds.
— prefers-reduced-motion only affects CSS transitions, not setInterval; a matchMedia check is needed to stop the timer.
— visibilitychange listener clears the interval when the page is hidden and resumes on return.
— Focus-based auto-pause creates a deadlock: clicking Resume leaves focus on the button, triggering the focus-pause logic again.
— The pause button is opacity:0, visible on hover, keyboard focus (:focus-within), or when paused, keeping the visual clean while remaining reachable.
— Unit tests increased from 923 to 926, and 14/14 real-device assertions passed, including 3→3 after pause and 3→2 in the control.
Conclusions

Treating autoplay as an accessibility problem rather than a visual parameter shifts the design goal: pausing must be explicit and reachable, not inferred from hover or focus.

Focus-based auto-pause has a fundamental deadlock: the control itself retains focus after activation, so any focus-driven pause logic immediately stops the resumed playback. An explicit state variable decouples intent from focus position.

aria-live is not additive; turning it on permanently makes screen reader output worse. The correct pattern is to silence it during automatic updates and enable it only for user-triggered changes.

prefers-reduced-motion is a CSS media query; it does not stop JavaScript timers. Developers who assume it handles autoplay will see the timer keep running, so a separate matchMedia check is required.

Concepts & terms
aria-live
An ARIA attribute that tells screen readers to announce changes to a region. Values include off, polite, and assertive; off silences automatic changes, polite announces after the user finishes current interaction.
prefers-reduced-motion
A CSS media query that detects whether the user has requested less animation. It only affects CSS transitions and animations, not JavaScript timers.
aria-roledescription
An ARIA attribute that provides a human-readable description for a role, e.g., 'carousel' instead of the generic 'region'.
WCAG 2.2.2
A Web Content Accessibility Guideline requiring that automatically updating content that lasts more than 5 seconds has a mechanism to pause, stop, or hide it.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗