这三个库(@vueuse/motion
、motion-v
、@motionone/vue
)都是为 Vue 3 设计的动画库,但它们的背景、定位和实现方式有所不同。以下是它们的核心关系和区别:
1. @vueuse/motion
-
定位:VueUse 生态中的动画扩展,轻量级、工具类风格。
-
特点 :
- 基于 Motion One(一个轻量级、高性能的 JavaScript 动画引擎)。
- 提供 Vue 指令(
v-motion
)和组件(<Motion>
)两种使用方式。 - 功能简洁,适合快速集成基础动画(如位移、透明度、缩放等)。
- 依赖
@vueuse/core
,可与其他 VueUse 工具链无缝配合。
-
示例 :
vue<template> <div v-motion="{ initial: { opacity: 0 }, enter: { opacity: 1, transition: { duration: 1000 } } }"> Hello World </div> </template>
2. motion-v
-
定位 :独立 Vue 动画库,灵感来自 Framer Motion(React 生态)。
-
特点 :
- 专为 Vue 3 设计,API 设计贴近 Vue 的响应式风格。
- 提供声明式动画组件(如
<motion-div>
)和指令。 - 支持弹性动画(Spring)、关键帧、手势交互等高级功能。
- 不依赖其他动画引擎,自成体系。
-
示例 :
vue<template> <motion-div :animate="{ x: 100 }" :transition="{ type: 'spring' }" > Move Me </motion-div> </template>
3. @motionone/vue
-
定位 :Motion One 官方提供的 Vue 适配层。
-
特点 :
- 直接封装 Motion One 的核心动画能力,保留其高性能特性(如硬件加速)。
- API 更接近原生 Motion One,适合需要精细控制动画的场景。
- 功能全面,支持时间轴(Timeline)、滚动触发(Scroll-triggered)等复杂动画。
- 需要搭配
motion
包使用(如import { animate } from 'motion'
)。
-
示例 :
vue<template> <div ref="target" class="box" /> </template> <script setup> import { ref, onMounted } from 'vue' import { animate } from 'motion' const target = ref() onMounted(() => { animate(target.value, { rotate: 360 }, { duration: 2, repeat: Infinity }) }) </script>
三者的关系对比
库名 | 底层引擎 | 设计灵感 | 适用场景 | 复杂度 |
---|---|---|---|---|
@vueuse/motion |
Motion One | VueUse 工具链 | 快速集成基础动画 | 低 |
motion-v |
独立实现 | Framer Motion | 声明式复杂交互动画 | 中 |
@motionone/vue |
Motion One | 原生 Motion | 高性能、精细控制的动画 | 高 |
如何选择?
- 简单动画 :选
@vueuse/motion
(与 VueUse 生态兼容性好)。 - 复杂交互 :选
motion-v
(API 更贴近 Vue 习惯,类似 Framer Motion)。 - 极致性能 :选
@motionone/vue
(直接控制 Motion One,适合动画密集型应用)。
共同点
- 均支持 Vue 3(部分可能兼容 Vue 2,但推荐用于 Vue 3)。
- 均基于现代浏览器动画 API(如
transform
、opacity
的 GPU 加速)。
如果有具体需求(如手势拖拽、滚动动画),可以进一步讨论哪个库最合适!