Skip to content

UseNumberAnimation

功能描述

UseNumberAnimation 是一个用于数字平滑过渡动画的 Vue 组合式函数,支持从一个值平滑过渡到另一个值,提供了多种缓动函数、动画控制和事件钩子。它基于 RAF (Request Animation Frame) 实现,确保动画流畅且性能高效。

安装

bash
# 使用 pnpm
pnpm add @oiij/use

# 使用 npm
npm install @oiij/use

# 使用 yarn
yarn add @oiij/use

依赖

  • vue: ^3.0.0
  • @vueuse/core: ^10.0.0

基本使用

loading

API

useNumberAnimation(options?)

创建数字动画。

参数

参数类型说明
optionsNumberAnimationOptions配置选项

NumberAnimationOptions

选项类型默认值说明
fromnumber0起始值
tonumber-目标值
durationnumber1000动画持续时间(毫秒)
precisionnumber0数字精度(小数位数)
easingEasingFunction'linear'缓动函数
manualbooleanfalse是否手动触发

返回值

属性类型说明
valueReadonly<Ref<string>>当前动画值
isActiveReadonly<Ref<boolean>>动画是否正在进行
start()Function开始动画
stop()Function停止动画
pause()Function暂停动画
resume()Function恢复动画
onStart(callback)Function动画开始事件
onEnd(callback)Function动画结束事件
onProgress(callback)Function动画进度事件

类型定义

ts
type EasingFunction = 'linear' | 'easeIn' | 'easeOut' | 'easeInOut' | ((t: number) => number)

export type NumberAnimationOptions = {
  /**
   * 起始值
   * @default 0
   */
  from?: number
  /**
   * 目标值
   */
  to?: number
  /**
   * 动画持续时间(毫秒)
   * @default 1000
   */
  duration?: number
  /**
   * 数字精度(小数位数)
   * @default 0
   */
  precision?: number
  /**
   * 缓动函数
   * @default 'linear'
   */
  easing?: EasingFunction
  /**
   * 是否手动触发
   * @default false
   */
  manual?: boolean
}

export type UseNumberAnimationReturns = {
  value: Readonly<Ref<string>>
  isActive: Readonly<Ref<boolean>>
  start: () => void
  stop: () => void
  pause: () => void
  resume: () => void
  onStart: (callback: () => void) => void
  onEnd: (callback: () => void) => void
  onProgress: (callback: (value: number) => void) => void
}

export declare function useNumberAnimation(options?: NumberAnimationOptions): UseNumberAnimationReturns

使用示例

基础用法

vue
<script setup>
import { useNumberAnimation } from '@oiij/use'

const { value, start, isActive } = useNumberAnimation({
  from: 0,
  to: 1000,
  duration: 2000
})
</script>

<template>
  <p>{{ value }}</p>
  <button :disabled="isActive" @click="start">
    开始动画
  </button>
</template>

带缓动函数

ts
import { useNumberAnimation } from '@oiij/use'

const { value } = useNumberAnimation({
  from: 0,
  to: 100,
  duration: 1000,
  easing: 'easeOut'
})

自定义缓动函数

ts
import { useNumberAnimation } from '@oiij/use'

const { value } = useNumberAnimation({
  from: 0,
  to: 100,
  duration: 1000,
  easing: t => t * t // 二次方缓动
})

手动控制

vue
<script setup>
import { useNumberAnimation } from '@oiij/use'

const { value, start, stop, pause, resume, isActive } = useNumberAnimation({
  from: 0,
  to: 1000,
  duration: 2000,
  manual: true
})
</script>

<template>
  <p>{{ value }}</p>
  <button @click="start">
    开始
  </button>
  <button @click="pause">
    暂停
  </button>
  <button @click="resume">
    继续
  </button>
  <button @click="stop">
    停止
  </button>
</template>

监听事件

ts
import { useNumberAnimation } from '@oiij/use'

const { value, onStart, onEnd, onProgress } = useNumberAnimation({
  from: 0,
  to: 1000,
  duration: 2000
})

onStart(() => {
  console.log('动画开始')
})

onEnd(() => {
  console.log('动画结束')
})

onProgress((value) => {
  console.log('当前值:', value)
})