动效原则
基础。每个创意技能调用时都会加载。 简洁规则在此。深入内容请见
references/。
时序规则
| 上下文 | 时长 | 原因 |
|---|---|---|
| 微交互(切换、悬停、聚焦) | 100-150ms | 即时反馈,无感知延迟 |
| UI 过渡(模态框、抽屉、标签切换) | 200-300ms | 流畅但不迟钝 |
| 页面/路由过渡 | 300-500ms | 建立空间叙事 |
| 滚动驱动 / 3D | 自由(基于进度) | 与用户输入绑定,无固定时长 |
频率规则: 动画播放得越频繁,它就必须越短且越微妙。 一个按钮悬停(每天 1000 次)= 100ms 的不透明度变化。一个引导页展示(仅一次)= 600ms+ 的完整编排。
缓动速查表
| 动作 | 缓动 | 原因 |
|---|---|---|
| 元素进入 | ease-out / spring | 减速进入静止位置(自然到达) |
| 元素退出 | ease-in | 加速离开(快速让开道路) |
| 元素在状态间移动 | ease-in-out | 平滑开始和停止 |
| 滚动同步 | linear / none | 与输入 1:1 匹配,无延迟感知 |
| 弹跳/趣味 | spring(欠阻尼) | 过冲创造活力 |
| 敏捷 UI | cubic-bezier(0.2, 0, 0, 1) | 快速启动,平滑着陆 |
退出始终要比进入更微妙。 进入:300ms ease-out,完整编排。退出:200ms ease-in,仅不透明度。
原生缓动等效(跨平台)
| Web(CSS / JS) | SwiftUI | Compose |
|---|---|---|
cubic-bezier(0.2, 0, 0, 1) | .spring(response: 0.4, dampingFraction: 0.85) 或 .snappy | spring(stiffness = Spring.StiffnessMedium, dampingRatio = 0.85f) |
ease-out | .easeOut(duration: 0.3) | tween(durationMillis = 300, easing = LinearOutSlowInEasing) |
ease-in | .easeIn(duration: 0.2) | tween(durationMillis = 200, easing = FastOutLinearInEasing) |
| spring(弹跳) | .bouncy(iOS 17+) | spring(stiffness = Spring.StiffnessLow, dampingRatio = Spring.DampingRatioMediumBouncy) |
| spring(平滑) | .smooth(iOS 17+) | spring(stiffness = Spring.StiffnessMedium, dampingRatio = Spring.DampingRatioNoBouncy) |
无障碍(不可协商)
减少动效 - 必须执行
每个动画组件都必须尊重用户的减少动效偏好。无例外,无论平台如何。
| 平台 | API |
|---|---|
| Web CSS | @media (prefers-reduced-motion: reduce) |
| Web JS | window.matchMedia('(prefers-reduced-motion: reduce)') |
| SwiftUI | @Environment(\.accessibilityReduceMotion) var reduceMotion |
| UIKit | UIAccessibility.isReduceMotionEnabled(+ reduceMotionStatusDidChangeNotification) |
| Compose | 使用 Settings.Global.ANIMATOR_DURATION_SCALE 的自定义辅助函数(深入内容见 mobile-principles/references/accessibility-mobile.md) |
CSS:
@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;
}
}
SwiftUI:
struct AnimatedView: View {
@Environment(\.accessibilityReduceMotion) var reduceMotion
@State var visible = false
var body: some View {
Text("Hello")
.opacity(visible ? 1 : 0)
.animation(reduceMotion ? .none : .spring(response: 0.4, dampingFraction: 0.85), value: visible)
}
}
Compose:
@Composable
fun rememberReduceMotion(): Boolean {
val context = LocalContext.current
return remember {
Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) == 0f
}
}
@Composable
fun AnimatedComponent(visible: Boolean) {
val reduce = rememberReduceMotion()
val alpha by animateFloatAsState(
targetValue = if (visible) 1f else 0f,
animationSpec = if (reduce) snap() else spring(stiffness = Spring.StiffnessMedium)
)
}
其他辅助功能要求
- 聚焦指示器绝不能因动画而隐藏
- 动画内容必须在每一帧都满足 WCAG 对比度(过渡过程中不能有文字淡出至不可见)
- 循环动画:提供暂停机制
通用“禁止”规则
1. 绝不动画化 width/height/top/left
每帧都会触发布局重新计算 = 卡顿。
/* 错误 */
.drawer { transition: height 0.3s ease; }
.drawer.open { height: 400px; }
/* 正确 */
.drawer { transition: transform 0.3s ease-out; transform: translateY(100%); }
.drawer.open { transform: translateY(0); }
SwiftUI 等效(错误 vs 正确):
// 错误 - 动画化 frame(引起布局传递)
.frame(height: open ? 400 : 0)
.animation(.easeInOut, value: open)
// 正确 - 通过 offset 动画化 transform
.offset(y: open ? 0 : 400)
.animation(.spring(), value: open)
Compose 等效(错误 vs 正确):
// 错误 - 动画化 size(完整布局传递)
val height by animateDpAsState(if (open) 400.dp else 0.dp)
// 正确 - 通过 graphicsLayer 动画化 Y 偏移
val translation by animateFloatAsState(if (open) 0f else 400f)
Box(modifier = Modifier.graphicsLayer { translationY = translation })
2. 绝不缩放到 0
scale(0) 会导致元素消失在黑洞中。始终保持一个最小值。
/* 错误 */
.modal-exit { transform: scale(0); }
/* 正确 */
.modal-exit { transform: scale(0.95); opacity: 0; }
SwiftUI 等效:
// 错误
.scaleEffect(visible ? 1.0 : 0.0)
// 正确
.scaleEffect(visible ? 1.0 : 0.95)
.opacity(visible ? 1.0 : 0.0)
Compose 等效:
// 错误
Modifier.graphicsLayer { scaleX = if (visible) 1f else 0f; scaleY = if (visible) 1f else 0f }
// 正确
Modifier.graphicsLayer {
scaleX = if (visible) 1f else 0.95f
scaleY = if (visible) 1f else 0.95f
alpha = if (visible) 1f else 0f
}
3. 绝不在进入时使用 ease-in
Ease-in = 启动缓慢。一个犹豫不决的进入元素感觉就像坏掉了。
/* 错误 */
.card-enter { animation: fadeIn 0.3s ease-in; }
/* 正确 */
.card-enter { animation: fadeIn 0.3s ease-out; }
/* 或者通过 JS 使用 spring 以获得自然感 */
4. UI 交互绝不超 500ms
模态框、下拉菜单、工具提示、标签页 —— 用户在等待。尊重他们的时间。
// 错误
gsap.to(modal, { opacity: 1, y: 0, duration: 0.8 });
// 正确
gsap.to(modal, { opacity: 1, y: 0, duration: 0.25, ease: "power2.out" });
SwiftUI:
// 错误
.animation(.easeInOut(duration: 0.8), value: state)
// 正确
.animation(.spring(response: 0.25, dampingFraction: 0.85), value: state)
Compose:
// 错误
animateContentSize(animationSpec = tween(800))
// 正确
animateContentSize(animationSpec = spring(stiffness = Spring.StiffnessMediumLow))
5. 绝不忽略 prefers-reduced-motion
这是无障碍要求,而不是锦上添花。
// 错误
gsap.from('.hero-title', { opacity: 0, y: 40, duration: 0.6 });
// 正确
const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!prefersReduced) {
gsap.from('.hero-title', { opacity: 0, y: 40, duration: 0.6 });
}
有关 SwiftUI / Compose 的等效项,请参见上面的跨平台减少动效章节。
性能清单
- 仅动画化
transform和opacity(仅合成属性) - 保守使用
will-change,动画完成后移除 - 对于 JS 动画,优先使用
requestAnimationFrame而非setTimeout/setInterval - 对于滚动驱动:CSS
animation-timeline> JSIntersectionObserver> 滚动监听器 - 在低端设备上测试(在 DevTools 中将 CPU 降速 4 倍)
快速参考:加载子技能
| 需求 | 加载 |
|---|---|
| 缓动深入内容,弹簧配置 | references/easing-guide.md |
| 可复制粘贴的进入/退出模式 | references/enter-exit-recipes.md |
| 设计师偏重的风格选择 | references/designers.md |
| 移动端 UX 上下文 | ../mobile-principles/SKILL.md |
| 桌面端 UX 上下文 | ../desktop-principles/SKILL.md |
| GSAP 特定内容 | ../gsap/SKILL.md |
| Framer Motion 特定内容 | ../framer-motion/SKILL.md |
| 纯 CSS 动画 | ../css-native/SKILL.md |
| Three.js / R3F | ../threejs-r3f/SKILL.md |
| Canvas / 生成式 | ../canvas-generative/SKILL.md |
| Compose Android 动画 | ../compose-motion/SKILL.md |
| Compose Multiplatform | ../compose-multiplatform/SKILL.md |
| SwiftUI iOS/macOS 动画 | ../swiftui-motion/SKILL.md |
| Compose 高级图形 | ../compose-graphics/SKILL.md |
| SwiftUI 高级图形 | ../swiftui-graphics/SKILL.md |
| 视觉 / 动效 / 辅助功能审计 | ../design-audit/SKILL.md |
| UI/UX 智能(84 种样式,192 种调色板) | ../ui-ux-pro-max/SKILL.md |