SuperAPI AI 工具集

motion

前端与建站

查看项目

更新于 10-16 01:15

技能介绍

用途:Motion(原 Framer Motion)是面向 React 和 JavaScript 的动画库,用于实现页面过渡、悬停/点击/拖拽等交互、布局变化与滚动联动动画。

主要能力:声明式动画通过 motion 组件的 initial、animate、exit 属性实现,无需手动操作 DOM;Variants 提供跨组件树共享的命名动画状态与自动错峰编排;AnimatePresence 让卸载组件保留到退出动画完成;layout 属性基于 FLIP 自动动画化 CSS 布局变化;内置 whileHover、whileTap、whileDrag、whileFocus 手势与回调;useMotionValue 与 useTransform 提供不触发渲染的可组合值;animate() 函数与 useAnimate 钩子提供命令式控制;默认使用弹簧物理过渡(type、stiffness、damping、mass 可配置);useScroll 与 useTransform 实现无需滚动事件监听的滚动动画。

使用方式:新项目安装 motion,React 中从 motion/react 导入,如 import { motion, AnimatePresence } from "motion/react";旧的 framer-motion 包仍可作为兼容层使用,但同一项目不要混用两者导入。非 React 或工具函数中使用从 motion 导入的 animate(),组件内复杂序列用 useAnimate。资料强调 Motion 各版本 API 会变化,编码前应先查阅 motion.dev 对应主题的在线文档,不要假设 API 存在。

限制与注意:AnimatePresence 的子元素必须有会变化的 key,否则退出动画不会执行;条件渲染应放在 AnimatePresence 内部,而非用外部 && 包裹;跨组件布局动画需用 LayoutGroup 包裹;useMotionValue 和 useTransform 的值不触发重渲染,不应在渲染逻辑中读取,应绑定到 motion 组件属性或用 useMotionValueEvent 订阅;motion 组件仅限 React,animate() 才是框架无关的命令式 API。具体收费、价格与平台支持资料未提供。