動畫與動態設計 是做什麼的?
創建流暢且具有目的性的動畫,以增強使用者體驗。
指示
- 動畫要有目的性 - 動作應引導使用者,而非造成干擾
- 保持快速 - 大多數 UI 動畫應在 150-300 毫秒之間
- 使用緩動曲線 - 絕不要在 UI 中使用線性計時
- 尊重使用者偏好 - 遵循
prefers-reduced-motion - 優化效能 - 僅為
transform和opacity屬性設定動畫
Framer Motion(推薦)
安裝
npm install framer-motion
基本動畫
import { motion } from 'framer-motion';
// 載入時淡入
function FadeIn({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.3 }}
>
{children}
</motion.div>
);
}
// 載入時向上滑入
function SlideUp({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.4, ease: 'easeOut' }}
>
{children}
</motion.div>
);
}
// 懸停時縮放
function ScaleButton({ children }: { children: React.ReactNode }) {
return (
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 400, damping: 17 }}
>
{children}
</motion.button>
);
}
進場/離場動畫
import { motion, AnimatePresence } from 'framer-motion';
function Modal({ isOpen, onClose, children }: ModalProps) {
return (
<AnimatePresence>
{isOpen && (
<>
{/* 背景遮罩 */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="fixed inset-0 bg-black/50"
onClick={onClose}
/>
{/* 模態框 */}
<motion.div
initial={{ opacity: 0, scale: 0.95, y: 20 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: 20 }}
transition={{ duration: 0.2, ease: 'easeOut' }}
className="fixed inset-0 flex items-center justify-center p-4"
>
<div className="bg-white rounded-xl shadow-xl max-w-md w-full p-6">
{children}
</div>
</motion.div>
</>
)}
</AnimatePresence>
);
}
清單動畫
import { motion, AnimatePresence } from 'framer-motion';
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: {
staggerChildren: 0.1,
},
},
};
const item = {
hidden: { opacity: 0, y: 20 },
show: { opacity: 1, y: 0 },
};
function AnimatedList({ items }: { items: Item[] }) {
return (
<motion.ul variants={container} initial="hidden" animate="show">
<AnimatePresence mode="popLayout">
{items.map((item) => (
<motion.li
key={item.id}
variants={item}
exit={{ opacity: 0, x: -100 }}
layout
>
<ItemCard {...item} />
</motion.li>
))}
</AnimatePresence>
</motion.ul>
);
}
佈局動畫
import { motion, LayoutGroup } from 'framer-motion';
function ExpandableCard({ id, title, content, isExpanded, onToggle }: Props) {
return (
<LayoutGroup>
<motion.div
layout
onClick={onToggle}
className="bg-white rounded-xl p-4 cursor-pointer"
>
<motion.h3 layout="position" className="font-semibold">
{title}
</motion.h3>
<AnimatePresence>
{isExpanded && (
<motion.div
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0 }}
transition={{ duration: 0.3 }}
>
<p className="mt-4 text-gray-600">{content}</p>
</motion.div>
)}
</AnimatePresence>
</motion.div>
</LayoutGroup>
);
}
滾動動畫
import { motion, useScroll, useTransform } from 'framer-motion';
function ParallaxHero() {
const { scrollY } = useScroll();
// 視差效果 - 圖片移動速度比滾動慢
const y = useTransform(scrollY, [0, 500], [0, 150]);
const opacity = useTransform(scrollY, [0, 300], [1, 0]);
return (
<div className="relative h-screen overflow-hidden">
<motion.div
style={{ y }}
className="absolute inset-0"
>
<img src="/hero.jpg" className="w-full h-full object-cover" />
</motion.div>
<motion.div
style={{ opacity }}
className="relative z-10 flex items-center justify-center h-full"
>
<h1 className="text-6xl font-bold text-white">歡迎</h1>
</motion.div>
</div>
);
}
// 滾動時揭露
function ScrollReveal({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true, margin: '-100px' }}
transition={{ duration: 0.6, ease: 'easeOut' }}
>
{children}
</motion.div>
);
}
手勢動畫
import { motion, useDragControls } from 'framer-motion';
function DraggableCard() {
return (
<motion.div
drag
dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
dragElastic={0.1}
whileDrag={{ scale: 1.05, cursor: 'grabbing' }}
className="w-48 h-48 bg-blue-500 rounded-xl cursor-grab"
/>
);
}
function SwipeToDelete({ onDelete }: { onDelete: () => void }) {
return (
<motion.div
drag="x"
dragConstraints={{ left: 0, right: 0 }}
onDragEnd={(_, info) => {
if (info.offset.x < -100) {
onDelete();
}
}}
className="bg-white p-4 rounded-lg"
>
向左滑動刪除
</motion.div>
);
}
載入狀態
骨架載入器
function Skeleton({ className = '' }: { className?: string }) {
return (
<div
className={`animate-pulse bg-gray-200 dark:bg-gray-700 rounded ${className}`}
/>
);
}
function CardSkeleton() {
return (
<div className="bg-white rounded-xl p-6 space-y-4">
<Skeleton className="h-6 w-3/4" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-5/6" />
<div className="flex gap-4 pt-4">
<Skeleton className="h-10 w-24" />
<Skeleton className="h-10 w-24" />
</div>
</div>
);
}
旋轉指示器
function Spinner({ size = 'md' }: { size?: 'sm' | 'md' | 'lg' }) {
const sizes = {
sm: 'w-4 h-4',
md: 'w-8 h-8',
lg: 'w-12 h-12',
};
return (
<svg
className={`animate-spin text-blue-600 ${sizes[size]}`}
viewBox="0 0 24 24"
fill="none"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
/>
</svg>
);
}
進度條
function ProgressBar({ value, max = 100 }: { value: number; max?: number }) {
const percentage = Math.min((value / max) * 100, 100);
return (
<div className="h-2 bg-gray-200 rounded-full overflow-hidden">
<motion.div
className="h-full bg-blue-600"
initial={{ width: 0 }}
animate={{ width: `${percentage}%` }}
transition={{ duration: 0.5, ease: 'easeOut' }}
/>
</div>
);
}
CSS 動畫
Tailwind 動畫
// Tailwind 內建動畫
<div className="animate-spin">旋轉</div>
<div className="animate-ping">脈衝</div>
<div className="animate-pulse">閃爍</div>
<div className="animate-bounce">彈跳</div>
// 在 tailwind.config.js 中自訂動畫
module.exports = {
theme: {
extend: {
animation: {
'fade-in': 'fadeIn 0.3s ease-out',
'slide-up': 'slideUp 0.4s ease-out',
'scale-in': 'scaleIn 0.2s ease-out',
},
keyframes: {
fadeIn: {
'0%': { opacity: '0' },
'100%': { opacity: '1' },
},
slideUp: {
'0%': { transform: 'translateY(20px)', opacity: '0' },
'100%': { transform: 'translateY(0)', opacity: '1' },
},
scaleIn: {
'0%': { transform: 'scale(0.95)', opacity: '0' },
'100%': { transform: 'scale(1)', opacity: '1' },
},
},
},
},
};
CSS 過渡效果
<button className="
bg-blue-600 text-white px-4 py-2 rounded-lg
transition-all duration-200 ease-out
hover:bg-blue-700 hover:shadow-lg hover:-translate-y-0.5
active:translate-y-0 active:shadow-md
">
懸停我
</button>
尊重使用者偏好
import { useReducedMotion } from 'framer-motion';
function AnimatedComponent() {
const shouldReduceMotion = useReducedMotion();
return (
<motion.div
initial={shouldReduceMotion ? false : { opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={shouldReduceMotion ? { duration: 0 } : { duration: 0.4 }}
>
內容
</motion.div>
);
}
// CSS 方法
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
效能優化技巧
- 僅為 transform 和 opacity 設定動畫 - 使用 GPU 加速
- 謹慎使用
will-change- 僅用於複雜動畫 - 避免佈局抖動 - 不要為 width/height 設定動畫
- 小心使用
layoutprop - 可能導致重新排版 - 為滾動處理函數去彈跳 - 防止畫面卡頓
// 良好 - GPU 加速
<motion.div animate={{ x: 100, opacity: 0.5 }} />
// 不佳 - 導致重新排版
<motion.div animate={{ width: 200, marginLeft: 100 }} />
使用時機
- 頁面過場與導航
- 載入和骨架狀態
- 互動式 UI 元素
- 回饋與確認
- 引導介紹與教學
- 資料視覺化轉場
備註
- Framer Motion 為 bundle 增加約 30kb(gzip 壓縮後)
- 簡單的懸停/焦點過渡效果請使用 CSS
- 以 0.25 倍速測試動畫,確保流暢度
- 考慮暈動症問題 - 避免過度移動
