使用 Motion 实现动画
所有动画工作均使用 Motion 库 (https://motion.dev)。
快速开始
React(主要):
import { motion } from "motion/react"
// 基础动画
<motion.div animate={{ opacity: 1, y: 0 }} />
// 带过渡配置
<motion.div
animate={{ x: 100 }}
transition={{ type: "spring", stiffness: 100 }}
/>
核心模式
1. 弹簧物理(推荐)
<motion.div
animate={{ scale: 1 }}
transition={{
type: 'spring',
stiffness: 260,
damping: 20,
}}
/>
弹簧预设:
- 柔和:
stiffness: 120, damping: 14 - 弹跳:
stiffness: 180, damping: 12 - 坚硬:
stiffness: 300, damping: 20
2. 手势交互
<motion.div
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
drag="x"
dragConstraints={{ left: -100, right: 100 }}
/>
3. 布局动画
<motion.div layout>{/* 内容位置或大小会变化 */}</motion.div>
4. 滚动联动效果
import { useScroll, useTransform } from "motion/react"
const { scrollYProgress } = useScroll()
const opacity = useTransform(scrollYProgress, [0, 1], [1, 0])
<motion.div style={{ opacity }} />
5. 子元素交错动画
<motion.ul
variants={{
visible: { transition: { staggerChildren: 0.07 } },
}}
>
{items.map((item) => (
<motion.li
key={item}
variants={{
hidden: { opacity: 0, y: 20 },
visible: { opacity: 1, y: 0 },
}}
/>
))}
</motion.ul>
6. 进入/退出动画
import { AnimatePresence } from 'motion/react';
<AnimatePresence>
{show && <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} />}
</AnimatePresence>;
动画属性
变换属性(GPU 加速,性能高效):
x, y, z- 平移scale, scaleX, scaleY- 缩放rotate, rotateX, rotateY, rotateZ- 旋转skew, skewX, skewY- 倾斜
视觉属性(谨慎使用,可能影响性能):
opacitybackground, backgroundColorcolorborderRadius
SVG 特定属性:
pathLength- 用于路径绘制动画pathOffset- 偏移路径pathSpacing- 虚线间的间距
无障碍支持
始终尊重用户偏好:
<motion.div
animate={{ x: 100 }}
transition={{
type: 'spring',
// 如果用户偏好减少动画,则禁用动画
duration: 0,
}}
/>
Motion 会自动处理 prefers-reduced-motion。弹簧效果会变为即时过渡。
性能提示
- 尽量仅对变换属性和透明度进行动画(GPU 加速)
- 使用
layout属性,而非手动对宽度/高度进行动画 - 避免在滚动期间进行动画
- 对复杂动画使用
will-change: transform(Motion 会自动处理) - 限制同时进行的动画数量 - 使用交错动画而非同时进行
设计原则(简要)
何时使用动画
- 状态切换(加载 → 成功)
- 空间变化(进入/退出视图)
- 吸引注意力(谨慎使用)
- 确认用户操作
何时不使用动画
- 重复操作(首次之后)
- 性能关键路径
- 用户控制的滚动
- 每一次交互(克制很重要)
时间指南
- 微交互:150-250毫秒
- 面板/模态框过渡:200-300毫秒
- 页面过渡:300-500毫秒
- 弹簧效果:让物理引擎决定时长
弹簧 vs 时长
- 交互元素、手势、布局变化使用弹簧
- 有意序列、编排、加载器使用时长的过渡
详细动画理念请参阅 principles.md。
常见模式
加载状态:
<motion.div
animate={{ rotate: 360 }}
transition={{
repeat: Infinity,
duration: 1,
ease: 'linear',
}}
/>
提示通知:
<motion.div
initial={{ x: 400, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
exit={{ x: 400, opacity: 0 }}
transition={{ type: 'spring', stiffness: 300, damping: 30 }}
/>
抽屉:
<motion.div
initial={{ x: '-100%' }}
animate={{ x: 0 }}
exit={{ x: '-100%' }}
transition={{ type: 'spring', damping: 25 }}
/>
手风琴:
<motion.div
animate={{ height: isOpen ? 'auto' : 0 }}
transition={{ type: 'spring', bounce: 0 }}
style={{ overflow: 'hidden' }}
/>
资源
- Motion 文档:https://motion.dev/docs/react
- Motion 示例:https://motion.dev/examples(100+ 免费示例)
- GitHub 仓库:https://github.com/motiondivision/motion
- 扩展资源:请参阅
resources.md获取精选的 animation.dev 课程和资料库
快速故障排除
动画未运行?
- 检查元素是否为条件渲染(使用 AnimatePresence)
- 确认目标值与初始值不同
- 检查是否存在可能覆盖的 CSS(例如
!important)
性能问题?
- 坚持使用变换属性和透明度
- 使用
layout属性替代对尺寸的动画 - 检查浏览器开发者工具的 Performance 面板
布局动画闪烁?
- 确保父元素有
position: relative - 必要时同时为父元素和子元素添加
layout - 检查是否存在冲突的 CSS 过渡
说明
- Motion 是 MIT 许可的,可投入生产使用
- 混合引擎:120fps,GPU 加速
- 内置 TypeScript 支持
- 支持 tree-shaking,优化打包体积
- 兼容 React 18+(包括 Suspense)