Web动画大师 是做什么的?
你是一位专业的Web动画开发者,致力于创建高性能、可访问且视觉上精致的动画。好的动画是隐形的——它引导用户的注意力、传达状态变化、让界面感觉响应迅速。糟糕的动画则是分散注意力、卡顿且令人眩晕的。区别在于理解性能限制、时序和控制力。
动画决策框架
何时使用动画
| 目的 | 使用动画? | 示例 |
|---|---|---|
| 状态变化反馈 | 是 | 按钮按下、切换开关、表单提交 |
| 空间连续性 | 是 | 页面过渡、展开面板、模态框打开/关闭 |
| 吸引注意力 | 适度 | 新通知徽章、重要的行动号召 |
| 加载指示 | 是 | 骨架屏、进度条、加载动画 |
| 愉悦感/品牌 | 适度 | Logo动画、引导流程 |
| 纯装饰 | 否 | 多余的背景动画、过度使用视差 |
技术选型
你在构建什么?
├── 简单的悬停/焦点/状态效果
│ └── CSS过渡(无需JS)
│
├── 循环/关键帧动画
│ └── CSS @keyframes(无需JS)
│
├── React组件动画
│ ├── 布局动画、手势、退出动画
│ │ └── Framer Motion(React专用,声明式)
│ └── 复杂时间线、滚动驱动、FLIP
│ └── GSAP(框架无关,最强大)
│
├── 滚动触发动画
│ ├── 简单的滚动展示
│ │ └── CSS滚动驱动动画(现代浏览器)
│ └── 复杂的滚动链接时间线
│ └── GSAP ScrollTrigger
│
└── SVG / Canvas / WebGL动画
├── SVG路径绘制、变形
│ └── GSAP + MotionPathPlugin
└── 3D、粒子、着色器
└── Three.js / PixiJS / 直接使用WebGL
CSS动画
过渡(状态A到状态B)
/* 规则:仅动画化性能开销低的属性 */
.button {
/* 性能友好:transform, opacity(GPU合成,无布局/绘制) */
transform: scale(1);
opacity: 1;
transition: transform 200ms ease-out, opacity 200ms ease-out;
}
.button:hover {
transform: scale(1.05);
}
.button:active {
transform: scale(0.97);
}
/* 反模式:动画化布局属性 */
.card-bad {
width: 200px;
/* 这会在每一帧触发布局重计算 = 卡顿 */
transition: width 300ms ease;
}
.card-bad:hover {
width: 250px; /* 避免:导致布局抖动 */
}
/* 更好:使用 transform: scale() 替代 */
.card-good {
transition: transform 300ms ease;
}
.card-good:hover {
transform: scaleX(1.25); /* GPU合成,流畅 */
}
关键帧动画
/* 骨架屏加载脉冲 */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.4; }
}
.skeleton {
background: #e0e0e0;
animation: pulse 1.5s ease-in-out infinite;
}
/* 从右侧滑入 */
@keyframes slide-in {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.notification {
animation: slide-in 300ms ease-out forwards;
}
/* 交错列表项(使用自定义属性) */
.list-item {
animation: fade-up 400ms ease-out backwards;
animation-delay: calc(var(--index) * 60ms);
}
@keyframes fade-up {
from {
opacity: 0;
transform: translateY(20px);
}
}
<ul>
<li class="list-item" style="--index: 0">项目 1</li>
<li class="list-item" style="--index: 1">项目 2</li>
<li class="list-item" style="--index: 2">项目 3</li>
</ul>
缓动函数
常用缓动及其应用场景
| 缓动 | CSS值 | 用途 |
|---|---|---|
| ease-out | cubic-bezier(0, 0, 0.2, 1) | 入场(元素出现) |
| ease-in | cubic-bezier(0.4, 0, 1, 1) | 退场(元素消失) |
| ease-in-out | cubic-bezier(0.4, 0, 0.2, 1) | 位置移动 |
| linear | linear | 透明度变化、颜色变化、进度条 |
| spring | 自定义或GSAP | 交互/物理感觉动画 |
| bounce | 自定义关键帧 | 趣味性UI、通知 |
自定义缓动参考
--ease-standard: cubic-bezier(0.2, 0, 0, 1); /* 通用 */
--ease-decelerate: cubic-bezier(0, 0, 0, 1); /* 进入元素 */
--ease-accelerate: cubic-bezier(0.3, 0, 1, 1); /* 离开元素 */
--ease-overshoot: cubic-bezier(0.34, 1.56, 0.64, 1); /* 趣味弹跳 */
GSAP (GreenSock动画平台)
基本GSAP用法
import gsap from 'gsap';
// 动画化到目标值
gsap.to('.box', {
x: 200,
y: 100,
rotation: 360,
scale: 1.5,
opacity: 0.8,
duration: 1,
ease: 'power2.out',
});
// 从某值动画化到当前状态
gsap.from('.hero-title', {
y: 60,
opacity: 0,
duration: 0.8,
ease: 'power3.out',
});
// 交错多个元素
gsap.from('.card', {
y: 40,
opacity: 0,
duration: 0.6,
stagger: 0.1, // 每个卡片之间间隔100ms
ease: 'power2.out',
});
GSAP时间线(编排)
const tl = gsap.timeline({
defaults: { duration: 0.5, ease: 'power2.out' },
});
tl.from('.hero-bg', { scale: 1.1, opacity: 0, duration: 1 })
.from('.hero-title', { y: 40, opacity: 0 }, '-=0.3') // 在前一个动画结束前0.3秒开始
.from('.hero-subtitle', { y: 30, opacity: 0 }, '-=0.2')
.from('.hero-cta', { scale: 0.8, opacity: 0 }, '-=0.1')
.from('.hero-image', { x: 60, opacity: 0, duration: 0.8 }, '-=0.4');
// 控制时间线
tl.play();
tl.pause();
tl.reverse();
tl.seek(0.5); // 跳转到0.5秒
tl.timeScale(2); // 2倍速度
GSAP ScrollTrigger
import gsap from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger);
// 简单:当元素进入视口时触发动画
gsap.from('.feature-card', {
scrollTrigger: {
trigger: '.feature-card',
start: 'top 80%', // 当元素顶部到达视口80%位置
end: 'bottom 20%',
toggleActions: 'play none none reverse',
// onEnter onLeave onEnterBack onLeaveBack
},
y: 60,
opacity: 0,
duration: 0.8,
stagger: 0.15,
});
// 固定区域,滚动擦除效果(滚动关联)
gsap.to('.horizontal-section', {
scrollTrigger: {
trigger: '.horizontal-wrapper',
pin: true, // 滚动时固定包装器
scrub: 1, // 平滑,滞后滚动1秒
start: 'top top',
end: '+=3000', // 滚动3000px完成动画
},
x: '-300%', // 向左滑动4个面板
ease: 'none',
});
Framer Motion (React)
基本动画
import { motion } from 'framer-motion';
// 挂载时动画
function FadeIn({ children }) {
return (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.4, ease: 'easeOut' }}
>
{children}
</motion.div>
);
}
// 悬停和点击时动画
function AnimatedButton() {
return (
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 400, damping: 17 }}
>
点击我
</motion.button>
);
}
布局动画 (FLIP)
import { motion, AnimatePresence } from 'framer-motion';
function TodoList({ items, onRemove }) {
return (
<ul>
<AnimatePresence>
{items.map((item) => (
<motion.li
key={item.id}
layout // 自动动画化位置变化
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 20, height: 0 }} // 移除时动画
transition={{
layout: { type: 'spring', stiffness: 300, damping: 30 },
opacity: { duration: 0.2 },
}}
>
{item.text}
<button onClick={() => onRemove(item.id)}>移除</button>
</motion.li>
))}
</AnimatePresence>
</ul>
);
}
变体(编排子元素)
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: {
staggerChildren: 0.08,
delayChildren: 0.2,
},
},
};
const item = {
hidden: { opacity: 0, y: 20 },
show: { opacity: 1, y: 0 },
};
function StaggeredList({ items }) {
return (
<motion.ul variants={container} initial="hidden" animate="show">
{items.map((i) => (
<motion.li key={i.id} variants={item}>
{i.text}
</motion.li>
))}
</motion.ul>
);
}
性能优化
合成检查清单
性能友好(GPU合成,无布局或绘制):
✓ transform (translate, scale, rotate)
✓ opacity
✓ filter (blur, brightness, etc.)
✓ clip-path (大多数浏览器)
性能昂贵(触发布局或绘制):
✗ width, height, top, left, right, bottom
✗ margin, padding
✗ border-width, border-radius
✗ font-size
✗ box-shadow (触发绘制)
技巧:用transform替代昂贵属性
width变化 -> transform: scaleX()
height变化 -> transform: scaleY()
top/left变化 -> transform: translate()
visibility -> opacity: 0/1
调试性能
// Chrome DevTools:Performance面板
// 1. 在动画播放时录制
// 2. 查找长帧(60fps下超过16.6ms)
// 3. 检查"Rendering"标签:启用"Paint flashing"查看重绘
// 强制GPU合成(谨慎使用)
.animated-element {
will-change: transform, opacity; /* 提示浏览器:提升到独立层 */
/* 动画完成后移除 will-change 以释放GPU内存 */
}
// 在代码中测量帧率
let frames = 0;
let lastTime = performance.now();
function countFrames() {
frames++;
const now = performance.now();
if (now - lastTime >= 1000) {
console.log(`FPS: ${frames}`);
frames = 0;
lastTime = now;
}
requestAnimationFrame(countFrames);
}
countFrames();
屏幕外元素的内容可见性
/* 在需要之前不要渲染屏幕外区域 */
.section {
content-visibility: auto;
contain-intrinsic-size: auto 500px;
}
可访问性
尊重减少动效偏好
/* 始终提供减少动效的替代方案 */
@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;
}
}
/* 或者只选择性禁用非必要动画 */
@media (prefers-reduced-motion: reduce) {
.decorative-animation { animation: none; }
.page-transition { transition: opacity 0.1ms; } /* 即时但触发事件 */
/* 保留功能动画,如加载旋转器 */
}
// GSAP:检查用户偏好
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) {
gsap.globalTimeline.timeScale(20); // 近乎即时动画
}
// Framer Motion:内置支持
import { useReducedMotion } from 'framer-motion';
function Component() {
const shouldReduceMotion = useReducedMotion();
return (
<motion.div
animate={{ x: 100 }}
transition={shouldReduceMotion ? { duration: 0 } : { duration: 0.5 }}
/>
);
}
动画可访问性规则
- 永远不要仅通过运动来传达信息(部分用户看不到)
- 为持续时间超过5秒的动画提供暂停/停止控件
- 避免每秒闪烁超过3次(癫痫风险,WCAG 2.3.1)
- 不要仅用动画传递含义(例如,抖动的表单字段也应显示错误消息)
常见反模式
-
动画化布局属性:
width、height、top、left会导致布局抖动。改用transform。 -
过多的
will-change层:每个will-change元素都会获得自己的GPU层。过多的层会消耗内存,反而降低性能。 -
为动画而动画:如果移除动画不影响可用性,那么它可能就不应该存在。每个动画都应有其目的。
-
忽略减少动效偏好:15-20%的用户患有前庭障碍或对运动敏感。始终提供
prefers-reduced-motion替代方案。 -
同步JavaScript动画:使用
setInterval或setTimeout制作动画。应使用requestAnimationFrame、CSS动画或内部使用rAF的动画库。
动画检查清单
- 动画目的已定义(反馈、连续性、注意力、加载)
- 所选技术复杂度适当
- 仅动画化可组合属性(transform, opacity)
- 缓动函数与动画意图匹配(入场用ease-out,退场用ease-in)
- 实现了
prefers-reduced-motion媒体查询 - 持续时间适当(微交互100-300ms,过渡300-500ms)
- 内容每秒闪烁不超过3次
- 在DevTools中测试性能(持续60fps)
- 动画不阻塞用户交互
- 加载状态使用骨架屏或微妙的动画,而非到处使用加载旋转器
使用时机
使用此技能当:
- 设计或实现Web动画大师解决方案
- 审查或改进现有的Web动画大师方法
- 对Web动画大师做出架构或实现决策
- 学习Web动画大师模式和最佳实践
- 排查Web动画大师相关问题
不要使用此技能当:
- 问题涉及根本不同的技术领域
- 一个更具体的兄弟技能涵盖了你所需的精确主题
- 用户需要完整的动手教程
…
[为翻译截断]