Framer Motion 技能
適用於 React 應用程式的生產級動畫。
快速開始
安裝
npm install framer-motion
pnpm add framer-motion
基本用法
import { motion } from "framer-motion";
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.5 }}
>
內容
</motion.div>
核心概念
範例
模板
快速參考
基本動畫
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
transition={{ duration: 0.3 }}
>
內容
</motion.div>
懸停與點擊
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: "spring", stiffness: 400, damping: 17 }}
>
點我
</motion.button>
變體
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1 }
}
};
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>
AnimatePresence(退出動畫)
import { AnimatePresence, motion } from "framer-motion";
<AnimatePresence mode="wait">
{isVisible && (
<motion.div
key="modal"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
模態框內容
</motion.div>
)}
</AnimatePresence>
滾輪觸發
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.5 }}
>
滾動到可見區域時觸發動畫
</motion.div>
拖曳
<motion.div
drag
dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
dragElastic={0.1}
>
拖曳我
</motion.div>
佈局動畫
<motion.div layout layoutId="shared-element">
佈局改變時會產生動畫的內容
</motion.div>
過渡類型
transition={{ duration: 0.3, ease: "easeOut" }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
transition={{ type: "spring", bounce: 0.25 }}
transition={{ type: "inertia", velocity: 50 }}
緩動函數
ease: "linear"
ease: "easeIn"
ease: "easeOut"
ease: "easeInOut"
ease: "circIn"
ease: "circOut"
ease: "circInOut"
ease: "backIn"
ease: "backOut"
ease: "backInOut"
ease: [0.17, 0.67, 0.83, 0.67]
減少動畫
始終尊重使用者偏好:
import { motion, useReducedMotion } from "framer-motion";
function Component() {
const prefersReducedMotion = useReducedMotion();
return (
<motion.div
initial={{ opacity: 0, y: prefersReducedMotion ? 0 : 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: prefersReducedMotion ? 0 : 0.3 }}
>
尊重動畫偏好設定
</motion.div>
);
}
const variants = {
initial: { opacity: 0 },
animate: { opacity: 1 },
};
<motion.div
variants={variants}
initial="initial"
animate="animate"
className="motion-reduce:transition-none"
>
常見模式
淡入向上
const fadeInUp = {
initial: { opacity: 0, y: 20 },
animate: { opacity: 1, y: 0 },
transition: { duration: 0.4 }
};
<motion.div {...fadeInUp}>內容</motion.div>
交錯列表
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1, delayChildren: 0.2 }
}
};
const item = {
hidden: { opacity: 0, x: -20 },
show: { opacity: 1, x: 0 }
};
模態框
<AnimatePresence>
{isOpen && (
<>
{/* 背景遮罩 */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/50"
onClick={onClose}
/>
{/* 模態框 */}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="fixed inset-x-4 top-1/2 -translate-y-1/2 ..."
>
模態框內容
</motion.div>
</>
)}
</AnimatePresence>
手風琴
<motion.div
initial={false}
animate={{ height: isOpen ? "auto" : 0 }}
transition={{ duration: 0.3, ease: "easeInOut" }}
className="overflow-hidden"
>
<div className="p-4">手風琴內容</div>
</motion.div>
最佳實踐
- 使用變體:程式碼更簡潔,易於編排
- 尊重減少動畫偏好:務必檢查
useReducedMotion
- 謹慎使用
layout:可能消耗較多效能,只在需要時使用
- 退出動畫:使用
AnimatePresence 包裹
- 互動使用 Spring:為懸停/點擊提供更自然的感覺
- 頁面轉場使用 Tween:時間控制更可預測
- 使用 GPU 加速屬性:優先使用
opacity、scale、x、y 而非 width、height