Skills2026年4月11日·1 分钟阅读

Motion (Framer Motion) — Modern Animation Library for React & JS

Motion (formerly Framer Motion) is a modern animation library for React and JavaScript with a simple declarative API. Hardware-accelerated transforms, gestures, layout animations, and SVG morphing — all in a tiny package.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install 89ca3afc-356f-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Motion provides declarative, hardware-accelerated animations for React with gestures and layouts.
§01

What it is

Motion (formerly Framer Motion) is a modern animation library for React and vanilla JavaScript. It provides a simple declarative API for hardware-accelerated transforms, gesture recognition, layout animations, scroll-triggered animations, and SVG path morphing. The library handles the complexity of performant animations while keeping the developer API minimal.

This tool is for frontend developers who need smooth, production-quality animations without writing raw CSS keyframes or JavaScript requestAnimationFrame loops.

§02

How it saves time or tokens

Motion abstracts animation complexity into a few props. What would require dozens of lines of CSS and JavaScript becomes a single <motion.div> component with animate, whileHover, and layout props. Gesture detection, spring physics, and layout change animations work out of the box. For AI-assisted frontend development, generating Motion animations requires minimal code.

§03

How to use

  1. Install Motion via npm.
  2. Import the motion component.
  3. Add animation props to your JSX elements.
  4. Motion handles the rest: physics, performance, and cleanup.
# Install Motion
npm install motion

# For React
npm install framer-motion
§04

Example

import { motion } from 'framer-motion'

function AnimatedCard() {
  return (
    <motion.div
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      exit={{ opacity: 0, y: -20 }}
      whileHover={{ scale: 1.05 }}
      whileTap={{ scale: 0.95 }}
      transition={{ type: 'spring', stiffness: 300 }}
      className='card'
    >
      <h2>Animated Card</h2>
      <p>This card animates in, responds to hover and tap.</p>
    </motion.div>
  )
}

// Layout animation: smooth transitions when list items reorder
function AnimatedList({ items }) {
  return (
    <ul>
      {items.map(item => (
        <motion.li
          key={item.id}
          layout
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
        >
          {item.name}
        </motion.li>
      ))}
    </ul>
  )
}
§05

Related on TokRepo

§06

Common pitfalls

  • Animating layout properties (width, height) can cause layout thrashing. Use transform properties (x, y, scale, rotate) whenever possible for GPU-accelerated performance.
  • The layout prop triggers re-renders. In lists with many items, this can cause performance issues. Use layoutId selectively.
  • Motion adds bundle size. If you only need simple CSS transitions, consider native CSS instead of importing the full library.
  • Exit animations require AnimatePresence wrapper. Forgetting it means components disappear without animation.
  • Spring animations look natural but can overshoot. Tune stiffness and damping values for your use case.
  • Review the official documentation before deploying to production to ensure compatibility with your specific environment and requirements.

常见问题

Is Motion the same as Framer Motion?+

Yes. The library was renamed from Framer Motion to Motion. The React package is still framer-motion on npm. The standalone JavaScript version is available as the motion package.

Does Motion work with Next.js?+

Yes. Motion works with Next.js including App Router and Server Components. The motion components are client components, so use the 'use client' directive in files that use animations.

How does Motion handle performance?+

Motion uses hardware-accelerated CSS transforms by default, runs animations on the compositor thread when possible, and batches DOM reads and writes. It avoids layout thrashing by preferring transform properties.

Can I animate between routes?+

Yes. Use AnimatePresence with your router to animate page transitions. Motion supports exit animations that play before the component unmounts, enabling smooth route transitions.

Does Motion support scroll-triggered animations?+

Yes. Motion provides useScroll, useMotionValueEvent, and whileInView for scroll-driven animations. Elements can animate as they enter or exit the viewport.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产