约 5-10% 的用户对屏幕动画有不同程度的敏感。这包括:
CSS 提供了 prefers-reduced-motion 媒体查询,让开发者尊重用户的系统设置。
用户可以在操作系统中开启"减少动画"选项:
当用户开启此设置后,prefers-reduced-motion: reduce 媒体查询会匹配。
Animate.css 从 v4 开始内置了 prefers-reduced-motion 支持:
@media (prefers-reduced-motion: reduce) { .animate__animated { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; } }
当用户开启了减少动画设置时,所有 Animate.css 动画会在几乎不可感知的时间内完成(0.01ms),效果等同于直接显示最终状态。animation-iteration-count: 1 确保无限循环的动画(如 animate__pulse animate__infinite)也只播放一次。
这是"降级到静态"的最简单方案。元素仍然存在动画属性,但动画时长极短,用户看不到过渡过程。
直接把动画时长设为 0.01ms 有时会导致问题——如果 JS 依赖 animationend 事件来执行后续逻辑(比如动画结束后隐藏元素),极短的动画可能导致事件时序混乱。
更稳妥的方案是保持动画功能但降低幅度:
@media (prefers-reduced-motion: reduce) { .animate__animated { /* 不完全禁用动画,而是简化 */ animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; } /* 但保留关键的状态变化效果 */ .animate__fadeIn, .animate__fadeOut { animation-duration: 0.2s !important; } }
或者用透明度动画替代位移动画:
@media (prefers-reduced-motion: reduce) { /* 将所有位移动画替换为简单的透明度切换 */ .animate__fadeInUp { animation-name: fadeIn !important; } .animate__slideInLeft { animation-name: fadeIn !important; } .animate__zoomIn { animation-name: fadeIn !important; } .animate__bounceIn { animation-name: fadeIn !important; } }
这样保留了元素"出现"的视觉提示,但去掉了可能引起眩晕的位移和缩放。
有时需要在 JS 中判断用户是否偏好减少动画:
// 检测用户是否偏好减少动画 var prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; function addAnimation(element, animationClass) { if (prefersReducedMotion) { // 直接显示元素,不播放动画 element.style.opacity = '1'; return; } element.classList.add('animate__animated', animationClass); } // 监听用户动态切换偏好 window.matchMedia('(prefers-reduced-motion: reduce)').addEventListener('change', function(e) { prefersReducedMotion = e.matches; if (prefersReducedMotion) { // 用户开启了减少动画,立即停止所有动画 document.querySelectorAll('.animate__animated').forEach(function(el) { el.style.animationPlayState = 'paused'; el.style.opacity = getComputedStyle(el).opacity; }); } });
WCAG(Web Content Accessibility Guidelines)建议:当页面包含动画内容时,应提供暂停、停止或隐藏的机制。
为 Animate.css 动画添加暂停控制:
<style> .animation-pause-btn { position: fixed; bottom: 20px; right: 20px; z-index: 9999; padding: 8px 16px; background: #333; color: white; border: none; border-radius: 20px; cursor: pointer; } .animation-pause-btn:hover { background: #555; } </style> <button class="animation-pause-btn" id="animToggle" onclick="toggleAnimations()"> ⏸ 暂停动画 </button> <script> var animationsPaused = false; function toggleAnimations() { animationsPaused = !animationsPaused; var elements = document.querySelectorAll('.animate__animated'); elements.forEach(function(el) { el.style.animationPlayState = animationsPaused ? 'paused' : 'running'; }); var btn = document.getElementById('animToggle'); btn.textContent = animationsPaused ? '▶ 恢复动画' : '⏸ 暂停动画'; } </script>
WCAG 2.1 规定:页面内容不应有每秒闪烁超过 3 次的闪烁。Animate.css 的 flash 动画名容易引起误解,但实际检查其 @keyframes,闪烁频率远低于 3 次/秒,符合规范。
不过,如果你创建了自定义的闪烁动画,需要计算频率:
/* 危险!每秒闪烁 5 次 */ @keyframes dangerousFlash { 0%, 50% { opacity: 1; } 25%, 75% { opacity: 0; } } .dangerous { animation: dangerousFlash 1s infinite; } /* 安全!每秒闪烁 2 次 */ @keyframes safePulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } } .safe { animation: safePulse 1s infinite; }
规则:单一动画周期内的"亮-暗"转换次数 ≤ 3,且总持续时间 ≥ 1 秒。同时,大面积闪烁比小面积闪烁风险更高。


prefers-reduced-motion。在全局 CSS 中加入媒体查询降级。animationend 事件而非 setTimeout(animationDuration)。aria-live="polite" 或 aria-live="assertive"。<!-- 动画通知需要被屏幕阅读器识别 --> <div class="toast animate__animated animate__fadeInRight" role="alert" aria-live="assertive"> 操作成功 </div> <!-- 装饰性动画不需要被读取 --> <div class="animate__animated animate__pulse" aria-hidden="true"> ✨ </div>
role="alert" 和 aria-live="assertive" 告诉屏幕阅读器"这个元素的内容发生了重要变化,需要立即通知用户"。装饰性动画用 aria-hidden="true" 避免干扰屏幕阅读器。