nanime
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesenanime
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 with raw imports, and don't reach for GSAP
or transition CSS. The effect almost always has a nanime path.
onMountedanimejs这些组合式函数会持有 Anime.js 实例,在响应式输入发生变化时重建实例,并在卸载时将其还原。它们会等待挂载完成,因此具备 SSR 安全性。
组合式函数和组件会自动导入并自动注册。
不要退回到在 中直接导入 的做法,也不要使用 GSAP 或 transition CSS。几乎所有效果都有对应的 nanime 实现。
onMountedanimejsPick the tool
选择合适的工具
| Building | Use | Details |
|---|---|---|
| Animating elements, refs, arrays, plain objects | | |
| Values set imperatively at speed (cursor follow, live counter) | | |
| Sequencing | | timelines.md |
| Scroll-driven animation | | timelines.md |
| Text split into lines, words, chars | | timelines.md |
| Scrambling or revealing text | | |
| SVG draw, morph, motion path | | timelines.md |
| Dragging with snap, bounds, axis locks | | lists.md for draggable list items |
| | transitions.md |
| | transitions.md, lists.md |
| Mounted elements changing position or size (FLIP) | | layout.md |
| Testing an animation without a browser | verifying.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、数组和普通对象添加动画 | | |
| 需要快速以命令式方式设置数值(光标跟随、实时计数器) | | |
| 编排动画序列 | | timelines.md |
| 滚动驱动动画 | | timelines.md |
| 将文本拆分为行、单词和字符 | | timelines.md |
| 扰乱或显现文本 | | |
| SVG 描边、变形和运动路径 | 在组合式函数中使用 | timelines.md |
| 带吸附、边界和轴锁定的拖拽 | | lists.md(适用于可拖拽的列表项) |
| | transitions.md |
| | transitions.md、lists.md |
| 已挂载元素改变位置或尺寸(FLIP) | | 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 directly.
animejs| Alias | Holds |
|---|---|
| Anime.js types ( |
| Anime.js utils ( |
| |
| |
| |
| Both of the above |
这些辅助函数不会自动导入。请从以下别名导入,切勿直接从 导入。
animejs| 别名 | 包含内容 |
|---|---|
| Anime.js 类型( |
| Anime.js 工具函数( |
| |
| |
| |
| 上述两者 |
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 wrongA template ref is during setup. A plain object is read once, then, so
Anime.js falls back to a default, such as watching instead of
your element. A getter is read again once the element exists.
nulldocument.bodyRead yourself: Anime.js does not unwrap Vue refs inside parameters.
Template refs are while most params take , so use
, not a cast.
.valueT | nullT | undefined?? undefinedts
useAnimeScroll(() => ({ target: section.value ?? undefined })) // correct
useAnimeScroll({ target: section.value }) // silently wrong模板 ref 在 setup 期间为 。普通对象只会被读取一次,因此 Anime.js 会退回到默认目标,例如监控 而不是你的元素。getter 会在元素存在后再次求值。
nulldocument.body请自行读取 :Anime.js 不会在参数内部自动解包 Vue ref。模板 ref 的类型为 ,而大多数参数接受 ,因此应使用 ,不要使用类型断言。
.valueT | nullT | undefined?? undefinedCall composables only in setup
setup仅在 setup 中调用组合式函数
ts
function onClick() {
useAnimate(box, { x: 100 }) // leaks: no effect scope, never reverted
}From a handler or per-frame callback, use from and
own the handle:
animate#nanime/utilsts
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/utilsanimatets
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 once per
animation; observers with the same params stay in step.
useAnimeScrollnanime 会在参数和时间线的 中将组合式函数的返回值解包为对应的 Anime.js 实例:
sync()ts
const scroll = useAnimeScroll(() => ({ target: section.value ?? undefined, sync: true }))
useAnimate(box, { x: 400, autoplay: scroll })一个滚动观察器只能驱动一个动画。将其交给第二个动画会从第一个动画中夺走该观察器,导致第一个动画冻结。每个动画只调用一次 ;参数相同的观察器会保持同步。
useAnimeScrollLive values do not tick in a template
模板中的实时值不会自动更新
{{ animation.progress }}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
组合式函数重写的文本不得重新渲染
useScrambleTextuseSplitTextvue
<script setup lang="ts">
useScrambleText(title, {}, () => ({ text: track.value.title }))
</script>
<template>
<h2 ref="title" v-once>{{ track.title }}</h2>
</template>For , keep and pass the new text through its
option.
useSplitTextv-oncehtmluseScrambleTextuseSplitTextvue
<script setup lang="ts">
useScrambleText(title, {}, () => ({ text: track.value.title }))
</script>
<template>
<h2 ref="title" v-once>{{ track.title }}</h2>
</template>对于 ,请保留 ,并通过它的 选项传入新文本。
useSplitTextv-oncehtmlScramble timing goes in the third argument
Scramble 的时序应放在第三个参数中
AnimeJS sets its own , and , so the
same keys in 's second argument are ignored. Keep the second
argument for playback and callbacks:
scrambleText()durationdelayeaseuseScrambleTextts
useScrambleText(title, { onComplete: next }, () => ({ text: text.value, duration: 2000 }))AnimeJS 的 会自行设置 、 和 ,因此 第二个参数中的同名字段会被忽略。第二个参数请保留用于播放控制和回调:
scrambleText()durationdelayeaseuseScrambleTextts
useScrambleText(title, { onComplete: next }, () => ({ text: text.value, duration: 2000 }))Give list and layout groups deps
deps为列表组和布局组提供 deps
depsWithout , and 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 by default:
deps<AnimeTransitionGroup><AnimeLayoutGroup>depsvue
<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. on an
turns its animation off. Each move still measures every
element inside the group, so keep item markup lean. On an
whose items only enter and leave, set
.
:deps="[]"<AnimeLayoutGroup><AnimeTransitionGroup>:move-animation="false"如果不提供 , 和 会在每次重新渲染时测量其中的每个元素,即使没有任何元素移动。如果父组件每次渲染都传入新的对象或数组 prop,就会导致它们每次都重新渲染。默认情况下请传入 :
deps<AnimeTransitionGroup><AnimeLayoutGroup>depsvue
<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.reducedMotionnuxt.config.ts'never''system'prefers-reduced-motion'always'useDraggableuseAnimeScrollupdateAppConfig({ nanime: { reducedMotion } })prefers-reduced-motion在 中, 可设置为 (默认值)、(遵循 )或 。当减少动态效果时,组合式函数、过渡和组产生的动画会直接跳转到最终状态。 和 不受影响。若要让访问者自行选择,请调用 。
不要在 nanime 调用周围添加你自己的 检查。
nuxt.config.tsnanime.reducedMotion'never''system'prefers-reduced-motion'always'useDraggableuseAnimeScrollupdateAppConfig({ nanime: { reducedMotion } })prefers-reduced-motionRebuilds 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 as the last argument. Only
{ keepTime: true },useAnimateanduseAnimeTimelinetake it.useScrambleText - For a value that retargets constantly, use .
useAnimatable
当响应式目标或参数发生变化时,组合式函数会还原旧实例,并从头创建一个新实例。未变化的参数会被跳过,因此 getter 即使每次返回一个内容相同的新对象,也不会产生额外开销。
- 若希望从中断处继续,请将 作为最后一个参数传入。只有
{ keepTime: true }、useAnimate和useAnimeTimeline支持该参数。useScrambleText - 对于目标持续变化的值,请使用 。
useAnimatable