nanime

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

nanime

nanime

The composables own an Anime.js instance, rebuild it when reactive inputs change, and revert it on unmount. They wait for mount, so they are SSR-safe. Composables and components are auto-imported and auto-registered.
Don't drop to
onMounted
with raw
animejs
imports, and don't reach for GSAP or transition CSS. The effect almost always has a nanime path.
这些组合式函数会持有 Anime.js 实例,在响应式输入发生变化时重建实例,并在卸载时将其还原。它们会等待挂载完成,因此具备 SSR 安全性。 组合式函数和组件会自动导入并自动注册。
不要退回到在
onMounted
中直接导入
animejs
的做法,也不要使用 GSAP 或 transition CSS。几乎所有效果都有对应的 nanime 实现。

Pick the tool

选择合适的工具

BuildingUseDetails
Animating elements, refs, arrays, plain objects
useAnimate
(
useWaapiAnimate
for the Web Animations API)
Values set imperatively at speed (cursor follow, live counter)
useAnimatable
Sequencing
useAnimeTimeline
timelines.md
Scroll-driven animation
useAnimeScroll
timelines.md
Text split into lines, words, chars
useSplitText
timelines.md
Scrambling or revealing text
useScrambleText
SVG draw, morph, motion path
#nanime/proxies/svg
inside a composable
timelines.md
Dragging with snap, bounds, axis locks
useDraggable
lists.md for draggable list items
v-if
/
v-show
/ keyed enter and leave, crossfades
<AnimeTransition>
transitions.md
v-for
add, remove, reorder, stagger
<AnimeTransitionGroup>
transitions.md, lists.md
Mounted elements changing position or size (FLIP)
<AnimeLayoutGroup>
, or
useAnimeLayout
to
await
it
layout.md
Testing an animation without a browserverifying.md
For an Anime.js parameter, return shape or util, read https://animejs.com/documentation. nanime passes those through unchanged. For nanime's own API, read https://nanimejs.netlify.app/llms-full.txt or query the docs MCP server at https://nanimejs.netlify.app/mcp.
构建内容使用详情
为元素、ref、数组和普通对象添加动画
useAnimate
(使用 Web Animations API 时用
useWaapiAnimate
)
需要快速以命令式方式设置数值(光标跟随、实时计数器)
useAnimatable
编排动画序列
useAnimeTimeline
timelines.md
滚动驱动动画
useAnimeScroll
timelines.md
将文本拆分为行、单词和字符
useSplitText
timelines.md
扰乱或显现文本
useScrambleText
SVG 描边、变形和运动路径在组合式函数中使用
#nanime/proxies/svg
timelines.md
带吸附、边界和轴锁定的拖拽
useDraggable
lists.md(适用于可拖拽的列表项)
v-if
/
v-show
/ 带 key 的进入与离开、交叉淡化
<AnimeTransition>
transitions.md
v-for
添加、移除、重排、错峰动画
<AnimeTransitionGroup>
transitions.md、lists.md
已挂载元素改变位置或尺寸(FLIP)
<AnimeLayoutGroup>
,或使用
useAnimeLayout
等待其完成
layout.md
在没有浏览器的情况下测试动画verifying.md
若需了解 Anime.js 的参数、返回结构或实用工具,请阅读 https://animejs.com/documentation。nanime 会原样传递这些内容。 若需了解 nanime 自身的 API,请阅读 https://nanimejs.netlify.app/llms-full.txt,或查询 https://nanimejs.netlify.app/mcp 处的文档 MCP 服务器。

Aliases

别名

Not auto-imported. Import helpers from these, never from
animejs
directly.
AliasHolds
#nanime/types
Anime.js types (
AnimationParams
,
JSAnimation
, …) and
AnimeTransitionStyle
#nanime/utils
Anime.js utils (
stagger
,
random
,
set
, …), plus
animate
and
createTimer
for handler code
#nanime/easings
spring
,
cubicBezier
,
steps
, …
#nanime/proxies/svg
createMotionPath
,
createDrawable
,
morphTo
#nanime/proxies/text
scrambleText
, for use inside animation parameters
#nanime/proxies
Both of the above
这些辅助函数不会自动导入。请从以下别名导入,切勿直接从
animejs
导入。
别名包含内容
#nanime/types
Anime.js 类型(
AnimationParams
、
JSAnimation
、…)以及
AnimeTransitionStyle
#nanime/utils
Anime.js 工具函数(
stagger
、
random
、
set
、…),以及供事件处理代码使用的
animate
和
createTimer
#nanime/easings
spring
、
cubicBezier
、
steps
、…
#nanime/proxies/svg
createMotionPath
、
createDrawable
、
morphTo
#nanime/proxies/text
scrambleText
,用于在动画参数内部使用
#nanime/proxies
上述两者

Rules

规则

Each of these fails without an error.
以下情况都会静默失效,不会抛出错误。

Parameters that read a template ref must be a getter

读取模板 ref 的参数必须是 getter

ts
useAnimeScroll(() => ({ target: section.value ?? undefined }))   // correct
useAnimeScroll({ target: section.value })                        // silently wrong
A template ref is
null
during setup. A plain object is read once, then, so Anime.js falls back to a default, such as watching
document.body
instead of your element. A getter is read again once the element exists.
Read
.value
yourself: Anime.js does not unwrap Vue refs inside parameters. Template refs are
T | null
while most params take
T | undefined
, so use
?? undefined
, not a cast.
ts
useAnimeScroll(() => ({ target: section.value ?? undefined }))   // correct
useAnimeScroll({ target: section.value })                        // silently wrong
模板 ref 在 setup 期间为
null
。普通对象只会被读取一次,因此 Anime.js 会退回到默认目标,例如监控
document.body
而不是你的元素。getter 会在元素存在后再次求值。
请自行读取
.value
:Anime.js 不会在参数内部自动解包 Vue ref。模板 ref 的类型为
T | null
,而大多数参数接受
T | undefined
,因此应使用
?? undefined
,不要使用类型断言。

Call composables only in
setup

仅在 setup 中调用组合式函数

ts
function onClick() {
  useAnimate(box, { x: 100 })   // leaks: no effect scope, never reverted
}
From a handler or per-frame callback, use
animate
from
#nanime/utils
and own the handle:
ts
import { animate } from '#nanime/utils'
import type { JSAnimation } from '#nanime/types'

const pulse = shallowRef<JSAnimation | null>(null)
onScopeDispose(() => pulse.value?.revert())

function highlight(el: HTMLElement) {
  pulse.value?.revert()
  pulse.value = animate(el, { scale: [1, 1.1, 1], duration: 400 })
}
Code of your own that reads an element must also wait for mount.
ts
function onClick() {
  useAnimate(box, { x: 100 })   // leaks: no effect scope, never reverted
}
在事件处理器或逐帧回调中,请使用
#nanime/utils
中的
animate
,并自行管理句柄:
ts
import { animate } from '#nanime/utils'
import type { JSAnimation } from '#nanime/types'

const pulse = shallowRef<JSAnimation | null>(null)
onScopeDispose(() => pulse.value?.revert())

function highlight(el: HTMLElement) {
  pulse.value?.revert()
  pulse.value = animate(el, { scale: [1, 1.1, 1], duration: 400 })
}
你自己的代码如果需要读取元素,也必须等待挂载完成。

Pass composable returns straight in

直接传入组合式函数的返回值

nanime unwraps a composable's return value to its Anime.js instance, in parameters and in a timeline's
sync()
:
ts
const scroll = useAnimeScroll(() => ({ target: section.value ?? undefined, sync: true }))
useAnimate(box, { x: 400, autoplay: scroll })
One scroll observer drives one animation. Handing it to a second animation steals it from the first, which freezes. Call
useAnimeScroll
once per animation; observers with the same params stay in step.
nanime 会在参数和时间线的
sync()
中将组合式函数的返回值解包为对应的 Anime.js 实例:
ts
const scroll = useAnimeScroll(() => ({ target: section.value ?? undefined, sync: true }))
useAnimate(box, { x: 400, autoplay: scroll })
一个滚动观察器只能驱动一个动画。将其交给第二个动画会从第一个动画中夺走该观察器,导致第一个动画冻结。每个动画只调用一次
useAnimeScroll
;参数相同的观察器会保持同步。

Live values do not tick in a template

模板中的实时值不会自动更新

{{ animation.progress }}
never updates; the return value tracks the instance, not the numbers inside it. Copy the value out from a callback:
ts
const progress = ref(0)
useAnimate(box, { x: 400, onUpdate: self => progress.value = self.progress })
{{ animation.progress }}
永远不会更新;返回值跟踪的是实例,而不是实例内部的数值。请通过回调将数值复制出来:
ts
const progress = ref(0)
useAnimate(box, { x: 400, onUpdate: self => progress.value = self.progress })

Text a composable rewrites must not re-render

组合式函数重写的文本不得重新渲染

useScrambleText
and
useSplitText
write into the element. If Vue also renders the changing value there, they overwrite each other. Render the first value once and hand changes to the composable:
vue
<script setup lang="ts">
useScrambleText(title, {}, () => ({ text: track.value.title }))
</script>

<template>
  <h2 ref="title" v-once>{{ track.title }}</h2>
</template>
For
useSplitText
, keep
v-once
and pass the new text through its
html
option.
useScrambleText
和
useSplitText
会直接写入元素。如果 Vue 也在同一位置渲染不断变化的值,两者就会相互覆盖。请只渲染一次初始值,并将后续变化交给组合式函数处理:
vue
<script setup lang="ts">
useScrambleText(title, {}, () => ({ text: track.value.title }))
</script>

<template>
  <h2 ref="title" v-once>{{ track.title }}</h2>
</template>
对于
useSplitText
,请保留
v-once
,并通过它的
html
选项传入新文本。

Scramble timing goes in the third argument

Scramble 的时序应放在第三个参数中

AnimeJS
scrambleText()
sets its own
duration
,
delay
and
ease
, so the same keys in
useScrambleText
's second argument are ignored. Keep the second argument for playback and callbacks:
ts
useScrambleText(title, { onComplete: next }, () => ({ text: text.value, duration: 2000 }))
AnimeJS 的
scrambleText()
会自行设置
duration
、
delay
和
ease
,因此
useScrambleText
第二个参数中的同名字段会被忽略。第二个参数请保留用于播放控制和回调:
ts
useScrambleText(title, { onComplete: next }, () => ({ text: text.value, duration: 2000 }))

Give list and layout groups
deps

为列表组和布局组提供
deps

Without
deps
,
<AnimeTransitionGroup>
and
<AnimeLayoutGroup>
measure every element inside them on each re-render, even when nothing moved. A parent that passes a new object or array prop on every render re-renders them each time. Pass
deps
by default:
vue
<AnimeTransitionGroup :deps="[]">       <!-- moves on add, remove, reorder -->
<AnimeTransitionGroup :deps="[view]">   <!-- also when view resizes items -->
<AnimeLayoutGroup :deps="[expanded]">   <!-- only when expanded changes -->
List every value whose change moves or resizes items.
:deps="[]"
on an
<AnimeLayoutGroup>
turns its animation off. Each move still measures every element inside the group, so keep item markup lean. On an
<AnimeTransitionGroup>
whose items only enter and leave, set
:move-animation="false"
.
如果不提供
deps
,
<AnimeTransitionGroup>
和
<AnimeLayoutGroup>
会在每次重新渲染时测量其中的每个元素,即使没有任何元素移动。如果父组件每次渲染都传入新的对象或数组 prop,就会导致它们每次都重新渲染。默认情况下请传入
deps
:
vue
<AnimeTransitionGroup :deps="[]">       <!-- moves on add, remove, reorder -->
<AnimeTransitionGroup :deps="[view]">   <!-- also when view resizes items -->
<AnimeLayoutGroup :deps="[expanded]">   <!-- only when expanded changes -->
请列出所有变化时会移动项目或改变项目尺寸的值。在
<AnimeLayoutGroup>
上设置
:deps="[]"
会关闭其动画。每次移动仍会测量组内的每个元素,因此应保持项目标记尽量精简。对于仅包含进入和离开项目的
<AnimeTransitionGroup>
,请设置
:move-animation="false"
。

Reduced motion is a module option

减少动态效果是模块选项

nanime.reducedMotion
in
nuxt.config.ts
takes
'never'
(default),
'system'
(follows
prefers-reduced-motion
) or
'always'
. When motion is reduced, animations from the composables, transitions and groups jump to their end state.
useDraggable
and
useAnimeScroll
are not affected. To let visitors choose, call
updateAppConfig({ nanime: { reducedMotion } })
. Don't add your own
prefers-reduced-motion
checks around nanime calls.
在
nuxt.config.ts
中,
nanime.reducedMotion
可设置为
'never'
(默认值)、
'system'
(遵循
prefers-reduced-motion
)或
'always'
。当减少动态效果时,组合式函数、过渡和组产生的动画会直接跳转到最终状态。
useDraggable
和
useAnimeScroll
不受影响。若要让访问者自行选择,请调用
updateAppConfig({ nanime: { reducedMotion } })
。 不要在 nanime 调用周围添加你自己的
prefers-reduced-motion
检查。

Rebuilds restart the animation

重建会重新开始动画

When a reactive target or parameter changes, the composable reverts the old instance and builds a new one, from the start. Unchanged params are skipped, so a getter returning an identical fresh object costs nothing.
  • To continue mid-flight instead, pass
    { keepTime: true }
    as the last argument. Only
    useAnimate
    ,
    useAnimeTimeline
    and
    useScrambleText
    take it.
  • For a value that retargets constantly, use
    useAnimatable
    .
当响应式目标或参数发生变化时,组合式函数会还原旧实例,并从头创建一个新实例。未变化的参数会被跳过,因此 getter 即使每次返回一个内容相同的新对象,也不会产生额外开销。
  • 若希望从中断处继续,请将
    { keepTime: true }
    作为最后一个参数传入。只有
    useAnimate
    、
    useAnimeTimeline
    和
    useScrambleText
    支持该参数。
  • 对于目标持续变化的值,请使用
    useAnimatable
    。