Communitygithub.com

無障礙動畫(減少動態、分級處理)

尊重使用者作業系統的「減少動態」設定,以分級(非全有全無)方式處理 CSS 與 JavaScript 動畫,兼顧無障礙與使用者體驗。

無障礙動畫(減少動態、分級處理) 是什麼?

這項技能旨在為網頁開發者提供一個務實、分級的動畫無障礙方案。它教導開發者不要因為使用者啟用「減少動態」(prefers-reduced-motion: reduce)就移除所有動畫,而是根據動畫對前庭系統的影響程度,將動畫分為三個層級:第一級(前庭觸發器)完全移除、第二級(軟化/縮短)以短淡入淡出取代、第三級(永遠保留)保留必要的回饋動畫。技能涵蓋 CSS 的媒體查詢閘門、JavaScript 的 matchMedia API、以及 GSAP、Framer Motion、Lenis 等常見動畫函式庫的對應模式。同時也提供 React 的 useReducedMotion 鉤子、WCAG 對應(2.3.3、C39、2.2.2)、以及一個可獨立運作的 HTML 驗證檔案,確保分級策略在正常與減少動態兩種模式下都能正確運作。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/nehakanjamala/personal_website/tree/HEAD/.agents/skills/accessible-animation

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

無障礙動畫(減少動態、分級處理) 是做什麼的?

將 prefers-reduced-motion: reduce 視為優雅降級,而非開關。作業系統層級的設定(macOS:設定 > 輔助使用 > 顯示器 > 減少動態;Windows:設定 > 輔助使用 > 視覺效果 > 動畫效果;iOS/Android 類似設定)指示會觸發前庭系統的動畫可能導致噁心、頭暈或偏頭痛。正確的做法是移除危險的動畫,同時保留方向提示。

使用時機

在網頁 UI 新增或稽核動畫時、使用者回報暈眩或 WCAG 審查結果時,或是在整合動畫函式庫(GSAP、Framer Motion、Lenis 平滑滾動、Anime.js、CSS keyframes)至無障礙產品時使用。

核心原則:分級動畫,而非移除殆盡

移除所有動畫是常見的過度修正。一個即時的狀態變更若沒有任何轉場,反而可能更令人困惑(元素瞬間跳動),並移除了有用的提示(焦點環、顯示進度的載入旋轉圖示)。將每個動畫歸類為三個層級:

  • 第一級 — 完全移除(前庭觸發器)。 視差效果;大面積的滑入/平移;大型元素的縮放/放大;3D 旋轉和轉動;持續自動播放的輪播與跑馬燈;平滑滾動劫持(Lenis/Locomotive);滾動驅動的固定場景;路徑動畫;任何移動畫面大部分區域或暗示深度/自身移動的動畫。
  • 第二級 — 軟化/縮短。 以短暫的不透明度淡入淡出(≤200ms)取代移動。減少移動距離與持續時間。保留轉場的事實(使元素不會瞬間跳動),但移除位移。將 600ms 的從左側滑入取代為 150ms 的交叉淡入淡出。
  • 第三級 — 永遠保留。 不透明度淡入淡出、顏色/背景轉場、焦點環轉場、載入指示器,以及對意義而言至關重要的動畫(WCAG 允許必要的動畫)。這些極少觸發前庭反應,因為它們不暗示自身移動。

CSS 層:媒體查詢閘門

以動畫為預設值,然後在減少動態的查詢下覆寫。偏好使用全域安全網加上有針對性的覆寫。

/* 全域安全網:中和失控的動畫,但不要盲目設為 0 秒,
   這可能破壞等待 transitionend/animationend 事件的 JavaScript。使用 0.01ms。 */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;   /* 停止無限旋轉/跑馬燈 */
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;          /* 殺死平滑滾動 */
  }
}

全域網路是備用方案。在此之上疊加有意的、分級的覆寫,使第三級動畫得以存活:

.card {
  transition: transform 400ms ease, opacity 400ms ease, background-color 200ms ease;
}

@media (prefers-reduced-motion: reduce) {
  .card {
    /* 第二級:移除 transform(移動),保留 opacity + color(第三級) */
    transition: opacity 150ms ease, background-color 150ms ease;
  }
  .parallax-layer { transform: none !important; }        /* 第一級:移除 */
  .hero-zoom      { animation: none !important; }        /* 第一級:移除 */
}

使用正向查詢 (prefers-reduced-motion: no-preference) 來選擇性啟用動畫,這是複雜效果最安全的寫法:

.hero { opacity: 1; } /* 預設可見且靜止 */
@media (prefers-reduced-motion: no-preference) {
  .hero { animation: zoom-in 1.2s ease both; } /* 僅在允許時執行動畫 */
}

JavaScript 層:matchMedia 閘門

CSS 無法控制 JavaScript 驅動的動畫(GSAP 時間軸、Framer 的 animate、Canvas/WebGL、Lenis)。透過 matchMedia 讀取相同的訊號,並對即時變更做出反應(使用者在無需重新載入的情況下切換作業系統設定)。

const REDUCE_QUERY = '(prefers-reduced-motion: reduce)';

export function prefersReducedMotion() {
  return typeof window !== 'undefined'
    && window.matchMedia
    && window.matchMedia(REDUCE_QUERY).matches;
}

// 即時更新:監聽以使切換作業系統設定立即生效。
export function onReducedMotionChange(cb) {
  const mq = window.matchMedia(REDUCE_QUERY);
  const handler = (e) => cb(e.matches);
  mq.addEventListener('change', handler);          // 現代 API
  return () => mq.removeEventListener('change', handler);
}

函式庫閘門,作為一個可重複使用的層

// GSAP — 使用 matchMedia():GSAP 在變更時還原條件設定。
import gsap from 'gsap';
const mm = gsap.matchMedia();
mm.add({
  motionOK: '(prefers-reduced-motion: no-preference)',
  motionReduce: '(prefers-reduced-motion: reduce)',
}, (ctx) => {
  const { motionOK } = ctx.conditions;
  if (motionOK) {
    gsap.from('.hero', { y: 80, opacity: 0, duration: 1 });   // 第一級移動
  } else {
    gsap.from('.hero', { opacity: 0, duration: 0.15 });        // 第二級淡入淡出
  }
});

// Lenis 平滑滾動 — 減少動態時不要實例化。
import Lenis from 'lenis';
let lenis = null;
if (!prefersReducedMotion()) {
  lenis = new Lenis();
  const raf = (t) => { lenis.raf(t); requestAnimationFrame(raf); };
  requestAnimationFrame(raf);
}

React:一個 useReducedMotion 鉤子

import { useState, useEffect } from 'react';

export function useReducedMotion() {
  const query = '(prefers-reduced-motion: reduce)';
  const get = () =>
    typeof window !== 'undefined' && window.matchMedia
      ? window.matchMedia(query).matches
      : false;
  const [reduced, setReduced] = useState(get);

  useEffect(() => {
    const mq = window.matchMedia(query);
    const onChange = () => setReduced(mq.matches);
    onChange();                                   // 水合後同步
    mq.addEventListener('change', onChange);
    return () => mq.removeEventListener('change', onChange);
  }, []);

  return reduced;
}
// Framer Motion 有自己的 useReducedMotion();上面的鉤子也能用。
import { motion } from 'framer-motion';
function Card() {
  const reduced = useReducedMotion();
  return (
    <motion.div
      initial={reduced ? { opacity: 0 } : { opacity: 0, y: 40 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: reduced ? 0.15 : 0.5 }}
    />
  );
}

將 SSR 渲染的狀態初始化為 false(動畫),並在 useEffect 中重新同步以避免水合不匹配;作業系統的值僅在客戶端可知。

WCAG 對應

  • 2.3.3 來自互動的動畫(AAA)。 由互動(滾動、懸停)觸發的動畫必須可禁用,除非是必要的。尊重 prefers-reduced-motion 是標準技術。
  • C39(CSS 技術)。 使用 prefers-reduced-motion 查詢來防止動畫。這是命名的充分技術。
  • 2.2.2 暫停、停止、隱藏(A)。 任何持續超過 5 秒的自動播放動畫必須提供暫停/停止功能。自動輪播和跑馬燈無論媒體查詢如何都違反此規定 — 請提供控制按鈕。

交付與驗證(獨立 HTML)

打包的輔助工具(scripts/):scripts/seek-shot.sh anim.html 0 1.5 3 凍結 ?t=N 掛鉤並為每個時刻截圖;scripts/contact-sheet.sh sheet.png frame-*.png 將它們拼貼成一個概覽用的聯絡表。請參閱 scripts/README.md。

對於一個自包含的示範,交付物是一個可直接在瀏覽器中開啟的 HTML 檔案 — 標記、CSS 以及任何 JavaScript(GSAP/matchMedia 通過 CDN)都內嵌,無需建置步驟。對於這個技能,驗證是重點:你必須在正常和減少動態兩種路徑下截圖,並確認分級是正確的。

輸出合約:

  • 一個 .html 檔案,包含完整的動畫及其分級的減少動態覆寫(CSS 查詢 + 用於 JavaScript 驅動動畫的 matchMedia/gsap.matchMedia())。
  • 一個搜尋掛鉤用於凍結影格,以及一種強制進入減少動態程式碼路徑以進行螢幕截圖的方法。

搜尋掛鉤 — 凍結影格、強制偏好設定。 ?t=N 凍結時間軸;?reduce=1 即使在作業系統設定關閉時也強制進入減少路徑(CSS 無法透過 JavaScript 切換,因此查詢與你用於 JavaScript 閘門的相同旗標,並在作業系統/瀏覽器偏好模擬下為 CSS 層截圖):

<script>
  const q = new URLSearchParams(location.search);
  const reduce = q.get("reduce") === "1"
    || window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  // 根據 `reduce` 條件性建構動畫(第一級移除,第二級軟化,第三級保留)
  const t = q.get("t");
  if (t !== null) { tl.pause(); tl.seek(parseFloat(t)); }      // GSAP;CSS:el.style.animationDelay=(-t)+"s"; playState="paused"
  window.__ready = true;
</script>

驗證循環 — 對兩種狀態截圖: 分別捕捉正常路徑和減少路徑,各在動畫進行中,並確認第一級動畫已消失,第二級是短淡入淡出,第三級存活。為 CSS 層模擬偏好設定(Playwright --reduced-motion=reduce;代理的瀏覽器工具也可以模擬):

npx playwright screenshot --wait-for-timeout=500 "file://$PWD/demo.html?t=0.6" normal.png
npx playwright screenshot --reduced-motion=reduce --wait-for-timeout=500 "file://$PWD/demo.html?t=0.6&reduce=1" reduced.png

在你完成之前:

  1. 可獨立開啟 — 無主控台錯誤,CDN(如果有的話)已載入。
  2. 凍結 + ?reduce=1(以及 --reduced-motion=reduce)可靠地執行兩個程式碼路徑。
  3. 在兩種狀態下截圖,動畫進行中 — 正常看起來有明確意圖,減少路徑移除了第一級/軟化了第二級。
  4. prefers-reduced-motion 被視為分級,而非開關 — 無瞬間跳動,第三級淡入淡出/焦點環/旋轉圖示保留;任何超過 5 秒的自動動畫仍有暫停控制(2.2.2)。
  5. 兩種路徑中的緩動曲線都有意圖 — 無意外的 linear,持續時間合理(減少淡入淡出 ≤200ms,0.01ms 而非 0s)。

快速參考

動畫類型層級減少動態時的行為
視差/深度1移除(transform: none)
大面積滑入/平移1移除或以淡入淡出取代
縮放/放大(大面積)1移除
旋轉/3D 旋轉1移除(animation: none)
自動輪播/跑馬燈1停止 + 提供暫停控制(2.2.2)
平滑滾動(Lenis)1不要實例化
小面積滑入/彈出2交叉淡入淡出 ≤200ms
不透明度淡入淡出3保留(如果時間長則縮短)
顏色/焦點環轉場3保留
載入旋轉圖示3保留(必要的回饋)

注意事項

  • animation-duration: 0s 可能會破壞等待 animationend/transitionend 事件的程式碼;使用 0.01ms 以便事件仍能觸發。
  • 全域的 * 重置單獨使用會過度移除第三級動畫;務必疊加有意的覆寫。
  • 忘記 animation-iteration-count: 1 會使無限旋轉以接近零的持續時間運行(CPU 耗損,仍在動畫)。
  • 沒有媒體查詢能涵蓋 JavaScript/Canvas/WebGL — 使用 matchMedia 進行閘控,否則循環永遠不會停止。
  • 切換作業系統設定不會觸發頁面重新載入;如果沒有 change 監聽器,頁面會保持其過時的狀態。
  • SSR:在渲染期間讀取 matchMedia 會拋出錯誤或不匹配;預設為動畫,並在效果中同步。
  • 自動播放的動畫即使在減少動態關閉時也需要一個可見的暫停控制(2.2.2 是 A 級)。

參考檔案

  • references/patterns.md — 完整的 GSAP matchMedia 拆卸、Framer Motion MotionConfig、Lenis 在即時切換時啟動/停止、一個可重複使用的 animateWithMotionPreference 包裝器、帶暫停控制的跑馬燈,以及一個分級稽核檢查清單。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

nehakanjamala/personal_website

This skill should be used when the user asks to "fix janky CSS animation", "make animation 60fps", "stop layout thrashing", "animate width/height/top/left smoothly", "convert animation to transform", "animate box-shadow performantly", "animate height auto", "FLIP animation", or "why is my scroll/hover animation choppy".

nehakanjamala/personal_website

Create, modify, debug, and deploy Apify Actors, and write their input and output schemas. Use when building an Actor from scratch, changing or troubleshooting Actor code, generating or updating .actor schema files, or pushing an Actor to the Apify platform. To wrap an existing non-Actor project, use apify-actorization instead.

nehakanjamala/personal_website

Convert existing projects into Apify Actors - serverless cloud programs. Actorize JavaScript/TypeScript (SDK with Actor.init/exit), Python (async context manager), or any language (CLI wrapper). Use when migrating code to Apify, wrapping CLI tools as Actors, or adding Actor SDK to existing projects.

nehakanjamala/personal_website

Deprecated. Output schema generation moved to the apify-actor-development skill; prefer that skill whenever it is available.

nehakanjamala/personal_website

Design and build an official Apify integration for a company's product - workflow-automation apps (Zapier/n8n-style), AI agent plugins (coding-agent skills+MCP bundles or OpenClaw/Hermes-style harnesses), AI framework packages (LangChain/LlamaIndex-style), or direct application clients via apify-client. Use when planning, creating, or reviewing an integration that exposes Apify Actors, runs, datasets, or key-value stores inside another product.

nehakanjamala/personal_website

Universal AI-powered web scraper for any platform. Scrape data from Instagram, Facebook, TikTok, YouTube, LinkedIn, X/Twitter, Google Maps, Google Search, Google Trends, Reddit, Airbnb, Yelp, and 15+ more platforms. Use for lead generation, brand monitoring, competitor analysis, influencer discovery, trend research, content analytics, audience analysis, review analysis, SEO intelligence, recruitment, or any data extraction task.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a glassmorphism effect", "frosted glass UI", "Apple liquid glass style", "frosted blur card", "translucent glass panel animation", "make a frosted nav bar", "build a glass modal/dialog", "animate a glass card on hover", or "add a refracting liquid-glass hero". Covers frosted translucent panels with backdrop-filter blur, edge/specular highlights, SVG liquid-glass refraction, motion on hover/scroll/enter, and accessible reduced-transparency fallbacks.

nehakanjamala/personal_website

This skill should be used when the user asks to "build a scroll animation", "add ScrollTrigger", "pin a section while scrolling", "scrub an animation to scroll", "create a hero timeline", "do a horizontal scroll section", "split text and animate words", "animate a layout change with GSAP Flip", "sync ScrollTrigger with Lenis", "Lenis smooth scroll jittery", "ScrollTrigger markers drift", "ScrollTrigger pins break with Lenis/Locomotive", or "set up smooth scroll with GSAP pinning". Covers GSAP timelines, ScrollTrigger, SplitText, Flip, and Lenis/Locomotive smooth-scroll sync for code-driven web motion.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a Lottie animation", "play a .lottie or .json animation", "integrate a Bodymovin/After Effects animation on web or mobile", "control Lottie playback / segments", "make a scroll-driven Lottie", "recolor a Lottie at runtime", or "export an AE animation to Lottie". Covers dotLottie/lottie-web integration, playback control, interactivity, theming, and the AE export checklist.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a hover/press effect", "animate a toggle or switch", "build an animated like button", "make a toast/snackbar slide in", "animate a drawer or modal", "animate list reordering or add/remove", "do a shared-element layout transition", or "polish UI feedback". Covers UI motion with Framer Motion (motion/react) and modern CSS.

nehakanjamala/personal_website

This skill should be used when the user asks to "animate page or route transitions", "add a page transition or crossfade between views", "animate route changes in Next.js App Router", "use the View Transitions API", "AnimatePresence exit doesn't fire on navigation", or "Framer Motion exit animation not working / exit animation before unmount in Next.js". Covers enter/exit page-transition animation with the View Transitions API and Framer Motion, for Next.js App Router and general SPA routing.

nehakanjamala/personal_website

Scrape web pages using Scrapling with anti-bot bypass (like Cloudflare Turnstile), stealth headless browsing, spiders framework, adaptive scraping, and JavaScript rendering. Use when asked to scrape, crawl, or extract data from websites; web_fetch fails; the site has anti-bot protections; write Python code to scrape/crawl; or write spiders.

nehakanjamala/personal_website

This skill should be used when the user asks to "animate an SVG", "make a line draw itself on", "do a stroke draw-on / signature animation", "morph one shape into another", "move an element along a path", "animate an icon/logo", or "animate an SVG gradient or filter". Covers stroke-dashoffset draw-on, path morphing, motion-along-path, and animated icons/gradients/filters via CSS, SMIL, and GSAP.

相關技能