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 }}
>
你好,世界
</motion.div>
// 在元件卸載時動畫(需要 AnimatePresence)
import { AnimatePresence } from "motion/react";
<AnimatePresence>
{isVisible && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
內容
</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 }}
/>
過渡類型
// 彈簧(物理數值預設)
transition={{ type: "spring", stiffness: 100, damping: 10 }}
// 補間(基於持續時間)
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 prop用於自動處理版面變化的動畫
- AnimatePresence是退出動畫的必要條件
- layoutId用於路由之間共享元素的轉場
- useScroll用於與滾動相關的動畫(視差、進度條)
- whileHover/whileTap用於互動回饋
- 透過變體
transition.staggerChildren交錯子元件 - 保持動畫微妙 — UI 互動的持續時間以 0.2-0.5 秒為佳
- GPU 加速 — Motion 預設使用變換
何時使用 Motion vs GSAP
| Motion | GSAP | |
|---|---|---|
| 框架 | React 優先 | 無關框架 |
| 風格 | 宣告式(JSX props) | 指令式(函式呼叫) |
| 版面動畫 | 內建(魔術) | 手動 |
| 滾動 | useScroll 鉤子 | ScrollTrigger 外掛 |
| 手勢 | 內建(拖曳、懸停、點按) | Draggable 外掛 |
| SVG 變形 | 有限 | MorphSVGPlugin |
| 套件大小 | 約 30KB | 約 25KB 核心 |
來源
- 儲存庫:github.com/motiondivision/motion
- 文件:motion.dev
- 範例:motion.dev/docs/react-quick-start