data-animate-image
Play/pause control for animated images. Add data-animate-image to any GIF, WebP, or APNG for a toggle button with automatic reduced-motion support.
Overview
The data-animate-image attribute adds a play/pause toggle button to animated images. Users can stop distracting animations with a single click, and the image automatically pauses when prefers-reduced-motion is active.
How It Works
- The init script wraps the
<img>in a.animate-image-wrappercontainer for positioning - A toggle
<button>is injected alongside the image - On load, the first frame is captured to a canvas and converted to a static PNG data URL
- When paused, the image
srcis swapped to the static frame; when played, it reverts to the original animated source - The
data-animate-image-pausedattribute tracks the paused state
Attributes
| Attribute | Type | Description |
|---|---|---|
data-animate-image |
boolean | Marks an <img> for animation control enhancement. |
data-animate-image-paused |
boolean | Set initially to start the image paused. Also set/removed automatically when the user toggles playback. |
data-animate-image-init |
boolean | Set automatically to prevent double-binding. Do not set manually. |
Start Paused
Add data-animate-image-paused to the image to start it in a paused state. The first frame is captured on load and displayed as a static image until the user clicks play.
Reduced Motion
The init script automatically pauses animated images when the user has requested reduced motion. This is detected via two mechanisms:
prefers-reduced-motion: reduce— the OS-level media querydata-motion-reducedattribute on<html>— for app-level motion controls
When either is active, images are auto-paused after the first frame is captured. The user can still manually play the animation using the toggle button.
Runtime changes to the prefers-reduced-motion media query are also detected — if the user enables reduced motion while viewing the page, animated images will pause automatically.
Dynamic Elements
Images added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.
Accessibility
- The toggle button has an
aria-labelthat updates to "Pause animation" or "Play animation" based on state - Respects
prefers-reduced-motion: reduceby auto-pausing — users who find motion distracting see a static image by default - Also respects a page-level
data-motion-reducedattribute on<html> - The toggle button is keyboard-accessible (focusable, activated with Enter/Space)
- Without JavaScript, the image displays normally — progressive enhancement ensures the animated content is never lost
- Falls back gracefully for cross-origin images where canvas capture may be blocked by CORS