Communitygithub.com

无障碍动画(减少动效,分级处理)

当用户要求“尊重 prefers-reduced-motion”、“让动画易于访问”、“修复前庭/晕动症问题”、“添加 useReducedMotion hook”、或“满足 WCAG 2.3.3 / C39”时使用。提供 CSS 和 JavaScript 中的分级(非全有或全无)减少动效模式。

无障碍动画(减少动效,分级处理) 是什么?

该技能提供了一种成熟的响应式方法,用于处理网页动画中的 `prefers-reduced-motion: reduce` 媒体查询。核心原则是分级处理动画,而不是全面移除:将动画分为三个层级——完全移除(前庭触发)、柔和/缩短(用短淡入淡出替换移动)和安全保留(不透明度、颜色、焦点环)。这种方法避免了常见的过度纠正陷阱,即移除所有动画,这可能导致元素突变和可访问性信息丢失。该技能为 CSS 过渡和动画提供了明确的实施方案,包括使用积极查询为效果选择加入运动的模式。它还涵盖了 JavaScript 驱动的动画库,如 GSAP(使用其内置的 matchMedia())、Framer Motion 和 Lenis,以及一个可重用的 React `useReducedMotion` hook。该技能映射到 WCAG 成功标准 2.3.3 和 C39,并包含一个验证框架,通过提供可链接的 `?t=N` 和 `?reduce=1` 参数的独立 HTML 演示来截图和比较正常和减少动效路径。它包括一个快速参考表,用于对常见运动类型进行分类,以及一个实用的“陷阱”部分,涵盖了 0s 持续时间导致事件不触发、SSR 渲染问题和无限循环动画等常见问题。

兼容平台~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 类似设置)表明前庭触发的运动会引起恶心、头晕或偏头痛。正确的响应是移除危险的动效,同时保留方向提示。

何时使用

在添加或审核网页用户界面中的动画时、当用户报告晕动症或 WCAG 发现时、或将动画库(GSAP、Framer Motion、Lenis 平滑滚动、Anime.js、CSS 关键帧)集成到可访问产品中使用。

核心原则:分级处理动效,不要一刀切

移除所有动画是一种常见的过度纠正。没有过渡的即时状态变化可能会更令人迷惑(元素瞬移),并且会移除有用的视觉提示(焦点环、表示进度的加载旋转指示器)。将每个动画分为三级:

  • 第1级 — 完全移除(前庭触发)。 视差;视口中大面积的滑动/平移;大元素的缩放/放大;3D 旋转和翻转;持续自动播放的轮播图和跑马灯;平滑滚动劫持(Lenis/Locomotive);滚动劫持的固定场景;路径动画;任何使屏幕大部分区域移动或暗示深度/自身运动的动画。
  • 第2级 — 柔和/缩短。 用短不透明度淡入淡出(≤200ms)替换移动。减少距离和持续时间。保留过渡的事实(这样元素就不会瞬移)但移除位移。将 600ms 的从左侧滑入替换为 150ms 的交叉淡入淡出。
  • 第3级 — 始终保留。 不透明度淡入淡出、颜色/背景过渡、焦点环过渡、加载指示器以及对理解至关重要的动画(WCAG 允许必要的动画)。这些很少会引发前庭反应,因为它们不暗示自身运动。

CSS 层:媒体查询门控

将动效作为默认值编写,然后在减少动效查询下覆盖。首选全局安全网加上针对性覆盖。

/* 全局安全网:中和失控的动效但不要盲目设置为 0s,
   这可能会破坏等待 transitionend/animationend 的 JS。使用 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;          /* 停止平滑滚动 */
  }
}

全局网是一个后备措施。在其之上添加有意的、分级的覆盖,以便第3级动效幸存:

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

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

使用积极查询 (prefers-reduced-motion: no-preference) 来选择加入动效,这是复杂效果的最安全模式:

.hero { opacity: 1; } /* 默认可见,静态 */
@media (prefers-reduced-motion: no-preference) {
  .hero { animation: zoom-in 1.2s ease both; } /* 仅在允许时添加动画 */
}

JS 层:matchMedia 门控

CSS 无法门控 JS 驱动的动画(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 });   // 第1级移动
  } else {
    gsap.from('.hero', { opacity: 0, duration: 0.15 });        // 第2级淡入淡出
  }
});

// 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 hook

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();上面的 hook 也能工作。
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 和任何 JS(GSAP/matchMedia 通过 CDN),无需构建步骤。对于这个技能,验证是关键:你必须截取正常和减少动效两种路径的屏幕截图,并确认分级是正确的。

输出契约:

  • 一个 .html 文件,包含完整的动效及其分级的减少动效覆盖(用于 CSS 驱动的动效的 CSS 查询 + 用于 JS 驱动的动效的 matchMedia/gsap.matchMedia())。
  • 一个用于冻结帧的搜索工具,以及一种强制减少动效代码路径以进行屏幕截图的方法。

搜索工具 — 冻结帧,强制偏好设置。 ?t=N 冻结时间线;?reduce=1 即使在操作系统关闭时也强制减少路径(CSS 无法从 JS 切换,因此查询你与 JS 门控相同的标志,并使用模拟的操作系统/浏览器偏好进行 CSS 层截图):

<script>
  const q = new URLSearchParams(location.search);
  const reduce = q.get("reduce") === "1"
    || window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  // 根据 `reduce` 有条件地构建动效(第1级移除,第2级柔和,第3级保留)
  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>

验证循环 — 截取两种状态的截图: 分别捕获正常路径和减少路径,均在动画中途,并确认第1级动效已消失,第2级是短淡入淡出,第3级幸存。模拟 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. 在两种状态下截图,均在动画中途 — 正常看起来有意图,减少后移除第1级/柔和第2级。
  4. prefers-reduced-motion 被尊重为分级处理,而不是一刀切的开关 — 元素不会瞬移,第3级淡入淡出/焦点环/旋转指示器保留;任何超过 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 以便事件仍然触发。
  • 单独的全局 * 重置过度移除了第3级动效;始终添加有意的覆盖。
  • 忘记 animation-iteration-count: 1 会让无限旋转以接近零的持续时间运行(CPU 空转,仍在动画)。
  • 没有媒体查询覆盖 JS/Canvas/WebGL — 使用 matchMedia 门控它们,否则循环永不停。
  • 切换操作系统设置不会触发页面重新加载;没有 change 监听器,页面会保持其过期状态。
  • SSR:在渲染期间读取 matchMedia 会抛出错误或不匹配;默认为动效并在 effect 中同步。
  • 自动播放的动效即使在减少动效关闭时也需要可见的暂停控件(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.

相关技能