Communitygithub.com

Motion (Framer Motion) — React UI 動畫函式庫

React 的宣告式動畫。支援彈簧動畫、手勢、版面動畫與頁面轉場。GPU 加速,可投入生產環境。

Motion (Framer Motion) — React UI 動畫函式庫 是什麼?

Motion (原 Framer Motion) 是一個專為 React 設計的宣告式 UI 動畫函式庫。它提供了一系列高階 API,讓開發者能夠輕鬆建立流暢的動畫效果,包括基本動畫、彈簧物理、手勢互動、版面動畫以及頁面轉場。其核心優勢在於 GPU 加速的渲染效能,以及與 React 生態系無縫整合的宣告式語法。支援 TypeScript、React,並提供 motion/react 作為導入路徑。此技能涵蓋了 Motion 的主要 API,如 motion 元件、AnimatePresence、variants、useScroll、useAnimate 等,並說明了使用情境與最佳實踐。開發者可以透過此技能快速掌握如何在其 React 專案中實現高效、優雅的動畫效果,並理解何時選擇 Motion 相較於其他函式庫(如 GSAP)更為合適。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ever-just/agentskills/tree/HEAD/skills/framer-motion

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

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 }}
/>

最佳實務

  1. 使用變體來編排父子動畫
  2. 彈簧物理產生自然的動畫效果(避免線性緩動)
  3. layout prop用於自動處理版面變化的動畫
  4. AnimatePresence是退出動畫的必要條件
  5. layoutId用於路由之間共享元素的轉場
  6. useScroll用於與滾動相關的動畫(視差、進度條)
  7. whileHover/whileTap用於互動回饋
  8. 透過變體 transition.staggerChildren 交錯子元件
  9. 保持動畫微妙 — UI 互動的持續時間以 0.2-0.5 秒為佳
  10. GPU 加速 — Motion 預設使用變換

何時使用 Motion vs GSAP

MotionGSAP
框架React 優先無關框架
風格宣告式(JSX props)指令式(函式呼叫)
版面動畫內建(魔術)手動
滾動useScroll 鉤子ScrollTrigger 外掛
手勢內建(拖曳、懸停、點按)Draggable 外掛
SVG 變形有限MorphSVGPlugin
套件大小約 30KB約 25KB 核心

來源

  • 儲存庫:github.com/motiondivision/motion
  • 文件:motion.dev
  • 範例:motion.dev/docs/react-quick-start

Individual skills in this repo

This repo contains 9 individual skills — each has its own dedicated page.

ever-just/agentskills

> Create animated logos, icons, and micro-interactions as lightweight Lottie JSON files. No After Effects needed — build animations entirely in code.

ever-just/agentskills

> A TypeScript library using generators to program animations with a real-time editor. Ideal for explainer videos, code walkthroughs, and educational content.

ever-just/agentskills

> Add animated subtitles and captions to Remotion videos from .SRT files. TikTok-style word-by-word highlights, karaoke effects, and more.

ever-just/agentskills

> Create videos programmatically using React components. The most mature framework for code-driven video production.

ever-just/agentskills

> Copy-paste video components for Remotion. Intros, text effects, transitions, and full scene templates. Like shadcn/ui but for video.

ever-just/agentskills

Extract actionable intelligence from video content across YouTube, Vimeo, and company websites. Video reveals what text sources cannot: facility scale, team composition, product demonstrations, signage, event attendance, and operational reality.

ever-just/agentskills

Optimize and embed an existing video as a fast, crisp, autoplaying hero/background/loop on a website. Use when adding or replacing a video on a marketing site and it is too heavy, will not autoplay (especially on mobile/Safari), shows a codec error, or looks soft — to transcode it to web-safe H.264 with the right pixel format, faststart, retina-aware sizing, and a poster frame, then embed and verify it actually plays. This is optimization + embedding of footage you already have, not creating/animating a video (for that use remotion/moviepy/motion-canvas). Use when the user says "add this video to the site", "the hero video is too big / won't play / isn't crisp", "replace the homepage video", or "make this loop on the landing page".

ever-just/agentskills

Full conversion audit for any homepage or landing page. Use when someone asks to "review my homepage," "audit my landing page," "why isn't my page converting," "check my website," or wants feedback on their marketing page. Requires URL or screenshot before proceeding.

ever-just/agentskills

Automatically fetch YouTube video transcripts, generate structured summaries, and send full transcripts to messaging platforms. Detects YouTube URLs and provides metadata, key insights, and downloadable transcripts.

相關技能