Motion(Framer Motion)— React UI 动画库
声明式 React 动画:弹性动画、手势、布局动画和页面过渡。GPU 加速,生产可用。
概述
- 仓库:github.com/motiondivision/motion(26K+ 星标)
- 语言:TypeScript / React
- 许可证:MIT
- 安装:
npm install motion - 文档:motion.dev
- 注意:Framer Motion 已更名为 Motion。请从
motion/react导入。
核心 API
基础动画
import { motion } from "motion/react";
// 挂载时动画
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
Hello World
</motion.div>
// 退出时动画(需要 AnimatePresence)
import { AnimatePresence } from "motion/react";
<AnimatePresence>
{isVisible && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
Content
</motion.div>
)}
</AnimatePresence>
弹性物理动画
<motion.div
animate={{ x: 100 }}
transition={{
type: "spring",
stiffness: 300,
damping: 20,
mass: 1,
}}
/>
// 预设
transition={{ type: "spring", bounce: 0.5 }} // 0 = 无弹跳,1 = 弹跳强烈
变体(编排动画)
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: {
staggerChildren: 0.1, // 子元素之间的延迟
delayChildren: 0.3, // 初始延迟
},
},
};
const item = {
hidden: { opacity: 0, y: 20 },
show: { opacity: 1, y: 0 },
};
<motion.ul variants={container} initial="hidden" animate="show">
{items.map((i) => (
<motion.li key={i} variants={item}>{i}</motion.li>
))}
</motion.ul>
手势
// 悬浮
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
点击我
</motion.button>
// 拖拽
<motion.div
drag // 启用双轴拖拽
dragConstraints={{ left: -100, right: 100, top: -50, bottom: 50 }}
dragElastic={0.2}
/>
布局动画
// 自动动画布局变化
<motion.div layout>
{isExpanded ? <ExpandedContent /> : <CollapsedContent />}
</motion.div>
// 共享布局动画(元素间形变)
<motion.div layoutId="card">
{/* 当 layoutId 匹配时,此元素在位置间形变 */}
</motion.div>
滚动动画
import { useScroll, useTransform, motion } from "motion/react";
function ParallaxSection() {
const { scrollYProgress } = useScroll();
const y = useTransform(scrollYProgress, [0, 1], [0, -200]);
const opacity = useTransform(scrollYProgress, [0, 0.5, 1], [1, 1, 0]);
return (
<motion.div style={{ y, opacity }}>
视差内容
</motion.div>
);
}
useAnimate(命令式)
import { useAnimate } from "motion/react";
function MyComponent() {
const [scope, animate] = useAnimate();
async function handleClick() {
await animate(".box", { scale: 1.5 }, { duration: 0.3 });
await animate(".box", { rotate: 360 }, { duration: 0.5 });
await animate(".box", { scale: 1, rotate: 0 });
}
return (
<div ref={scope}>
<div className="box" onClick={handleClick}>点击我</div>
</div>
);
}
关键帧
<motion.div
animate={{
x: [0, 100, 100, 0, 0],
y: [0, 0, 100, 100, 0],
rotate: [0, 90, 180, 270, 360],
}}
transition={{ duration: 2, repeat: Infinity }}
/>
过渡类型
// Spring(默认用于物理值)
transition={{ type: "spring", stiffness: 100, damping: 10 }}
// Tween(基于时间)
transition={{ type: "tween", duration: 0.5, ease: "easeInOut" }}
// 缓动选项
ease: "linear" | "easeIn" | "easeOut" | "easeInOut"
ease: [0.42, 0, 0.58, 1] // 自定义三次贝塞尔
// 重复
transition={{ repeat: Infinity, repeatType: "reverse", duration: 1 }}
// 延迟
transition={{ delay: 0.5 }}
常用模式
页面过渡
// 在布局组件中
<AnimatePresence mode="wait">
<motion.div
key={pathname}
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -20 }}
transition={{ duration: 0.3 }}
>
{children}
</motion.div>
</AnimatePresence>
交错列表
const container = {
hidden: {},
show: { transition: { staggerChildren: 0.08 } },
};
const item = {
hidden: { opacity: 0, y: 15 },
show: { opacity: 1, y: 0, transition: { type: "spring", bounce: 0.3 } },
};
悬浮缩放卡
<motion.div
whileHover={{ scale: 1.03, boxShadow: "0 10px 30px rgba(0,0,0,0.15)" }}
transition={{ type: "spring", stiffness: 400, damping: 25 }}
/>
最佳实践
- 使用变体 编排父/子动画
- 弹性物理 提供自然的感觉(避免线性缓动)
- layout 属性 实现自动布局变化动画
- AnimatePresence 是退出动画必需的
- layoutId 用于路由间的共享元素过渡
- useScroll 用于与滚动关联的动画(视差、进度条)
- whileHover/whileTap 提供交互反馈
- 交错子元素 通过变体的
transition.staggerChildren - 保持动画微妙 —— UI 交互持续 0.2-0.5 秒
- GPU 加速 —— Motion 默认使用 transform
何时使用 Motion vs GSAP
| Motion | GSAP | |
|---|---|---|
| 框架 | 以 React 为先 | 框架无关 |
| 风格 | 声明式(JSX 属性) | 命令式(函数调用) |
| 布局动画 | 内置(神奇) | 手动处理 |
| 滚动 | useScroll 钩子 | ScrollTrigger 插件 |
| 手势 | 内置(拖拽、悬浮、点击) | Draggable 插件 |
| SVG 形变 | 有限 | MorphSVGPlugin |
| 包体积 | ~30KB | ~25KB 核心 |
来源
- 仓库:github.com/motiondivision/motion
- 文档:motion.dev
- 示例:motion.dev/docs/react-quick-start