Communitygithub.com

Motion (Framer Motion) — React UI 动画库

声明式 React 动画:弹性动画、手势、布局动画和页面过渡。GPU 加速,生产可用。

Motion (Framer Motion) — React UI 动画库 是什么?

Motion(前身为 Framer Motion)是一个基于 React 的声明式 UI 动画库,提供弹性物理动画、手势交互、布局动画和页面过渡等能力。其核心 API 包括 `motion` 组件、`AnimatePresence`、`useScroll` 和 `useAnimate` 等,支持通过 JSX 属性(如 `initial`、`animate`、`exit`)直接描述动画状态。Motion 在底层默认使用 GPU 加速的 transform 操作,性能优秀,适合构建流畅的交互界面。支持的平台包括 React 应用(Web),通过 `npm install motion` 安装并从 `motion/react` 导入。它特别适合需要复杂编排动画(如子元素交错进入)、手势反馈(拖拽、悬浮、点击)及自动布局过渡的场景,是替代 GSAP 的 React 原生选择。

兼容平台~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 }}
>
  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 }}
/>

最佳实践

  1. 使用变体 编排父/子动画
  2. 弹性物理 提供自然的感觉(避免线性缓动)
  3. layout 属性 实现自动布局变化动画
  4. AnimatePresence 是退出动画必需的
  5. layoutId 用于路由间的共享元素过渡
  6. useScroll 用于与滚动关联的动画(视差、进度条)
  7. whileHover/whileTap 提供交互反馈
  8. 交错子元素 通过变体的 transition.staggerChildren
  9. 保持动画微妙 —— UI 交互持续 0.2-0.5 秒
  10. GPU 加速 —— Motion 默认使用 transform

何时使用 Motion vs GSAP

MotionGSAP
框架以 React 为先框架无关
风格声明式(JSX 属性)命令式(函数调用)
布局动画内置(神奇)手动处理
滚动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.

相关技能