Vue3数值动画(NumberAnimation)

简介: 该文档介绍了一个基于 Vue 的数值动画组件 `NumberAnimation`,提供了丰富的配置选项,如起始值、目标值、动画时长等,并支持自定义前缀、后缀及样式。通过简单的方法和事件,可以轻松控制动画的播放与停止。

效果如下图:在线预览

在这里插入图片描述
在这里插入图片描述

APIs

NumberAnimation

参数 说明 类型 默认值
from 数值动画起始数值 number 0
to 数值目标值 number 1000
duration 数值动画持续时间,单位 ms number 3000
autoplay 是否自动开始动画 boolean true
precision 精度,保留小数点后几位 number 0
prefix 前缀 string undefined
suffix 后缀 string undefined
separator 千分位分隔符 string ‘,’
decimal 小数点字符 string ‘.’
valueStyle 数值文本样式 CSSProperties {}
transition 动画过渡效果 TransitionFunc TransitionFunc[‘easeInOutCubic’]

Methods

事件名称 说明 参数
play 播放动画 () => void

Events

事件名称 说明 参数
started 动画开始播放 () => void
finished 动画播放完成 () => void

安装依赖:

pnpm add @vueuse/core

创建数值动画组件NumberAnimation.vue

其中引入使用了以下工具函数:

<script setup lang="ts">
import { ref, computed, watchEffect, onMounted, watch } from 'vue'
import type { CSSProperties } from 'vue'
import { formatNumber } from '../utils'
import { useTransition, TransitionPresets } from '@vueuse/core'
enum TransitionFunc {
  linear = 'linear',
  easeOutSine = 'easeOutSine',
  easeInOutSine = 'easeInOutSine',
  easeInQuad = 'easeInQuad',
  easeOutQuad = 'easeOutQuad',
  easeInOutQuad = 'easeInOutQuad',
  easeInCubic = 'easeInCubic',
  easeOutCubic = 'easeOutCubic',
  easeInOutCubic = 'easeInOutCubic',
  easeInQuart = 'easeInQuart',
  easeOutQuart = 'easeOutQuart',
  easeInOutQuart = 'easeInOutQuart',
  easeInQuint = 'easeInQuint',
  easeOutQuint = 'easeOutQuint',
  easeInOutQuint = 'easeInOutQuint',
  easeInExpo = 'easeInExpo',
  easeOutExpo = 'easeOutExpo',
  easeInOutExpo = 'easeInOutExpo',
  easeInCirc = 'easeInCirc',
  easeOutCirc = 'easeOutCirc',
  easeInOutCirc = 'easeInOutCirc',
  easeInBack = 'easeInBack',
  easeOutBack = 'easeOutBack',
  easeInOutBack = 'easeInOutBack'
}
interface Props {
  from?: number // 数值动画起始数值
  to?: number // 数值目标值
  duration?: number // 数值动画持续时间,单位 ms
  autoplay?: boolean // 是否自动开始动画
  precision?: number // 精度,保留小数点后几位
  prefix?: string // 前缀
  suffix?: string // 后缀
  separator?: string // 千分位分隔符
  decimal?: string // 小数点字符
  valueStyle?: CSSProperties // 数值文本样式
  transition?: TransitionFunc // 动画过渡效果
}
const props = withDefaults(defineProps<Props>(), {
  from: 0,
  to: 1000,
  duration: 3000,
  autoplay: true,
  precision: 0,
  prefix: undefined,
  suffix: undefined,
  separator: ',',
  decimal: '.',
  valueStyle: () => ({}),
  transition: TransitionFunc['easeInOutCubic']
})
const source = ref(props.from)
watchEffect(() => {
  source.value = props.from
})
watch([() => props.from, () => props.to], () => {
  if (props.autoplay) {
    play()
  }
})
onMounted(() => {
  props.autoplay && play()
})
const outputValue = useTransition(source, {
  duration: props.duration,
  transition: TransitionPresets[props.transition],
  onFinished: () => emits('finished'),
  onStarted: () => emits('started')
})
function play() {
  source.value = props.to
}
const showValue = computed(() => {
  const { precision, separator, decimal, prefix, suffix } = props
  return formatNumber(outputValue.value, precision, separator, decimal, prefix, suffix)
})
const emits = defineEmits(['started', 'finished'])
defineExpose({
  play
})
</script>
<template>
  <span :style="valueStyle">
    {
  
  { showValue }}
  </span>
</template>

在要使用的页面引入

<script setup lang="ts">
import NumberAnimation from 'NumberAnimation.vue'
import { ref } from 'vue'

function onStarted () {
  console.log('started')
}
function onFinished () {
  console.log('finished')
}
const animationRef = ref()
function onClick () {
  animationRef.value.play()
}
</script>
<template>
  <div>
    <h1>NumberAnimation 数值动画</h1>
    <h2 class="mt30 mb10">基本使用</h2>
    <Row>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation :to="100000000.12345" />
        </Statistic>
      </Col>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation :to="100000000.12345" separator="" />
        </Statistic>
      </Col>
    </Row>
    <h2 class="mt30 mb10">精度</h2>
    <Row>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation :from="0.00" :to="100000000.12345" :precision="2" />
        </Statistic>
      </Col>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation :to="100000000.12345" :precision="3"/>
        </Statistic>
      </Col>
    </Row>
    <h2 class="mt30 mb10">自定义前缀 & 后缀</h2>
    <Row>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation
            prefix="$"
            :from="0"
            :to="100000000" />
        </Statistic>
      </Col>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation
            :from="0"
            :to="100000000"
            suffix="元" />
        </Statistic>
      </Col>
    </Row>
    <h2 class="mt30 mb10">自定义样式</h2>
    <Row>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation :value-style="{fontSize: '30px', fontWeight: 600, color: '#d4380d'}" :from="0" :to="100000000"/>
        </Statistic>
      </Col>
    </Row>
    <h2 class="mt30 mb10">自定义播放和动画时间</h2>
    <Row>
      <Col :span="12">
        <Statistic title="一个小目标">
          <NumberAnimation
            ref="animationRef"
            :from="0"
            :to="100000000"
            :duration="5000"
            :precision="2"
            :autoplay="false"
            @started="onStarted"
            @finished="onFinished" />
        </Statistic>
        <Button @click="onClick">播放</Button>
      </Col>
    </Row>
  </div>
</template>
相关文章
|
9天前
|
JavaScript API
Vue3中的计算属性能否动态修改
【9月更文挑战第5天】Vue3中的计算属性能否动态修改
40 10
|
2天前
|
JavaScript
Vue3中路由跳转的语法
Vue3中路由跳转的语法
105 58
|
5天前
|
前端开发
vue3+ts项目中使用mockjs
vue3+ts项目中使用mockjs
198 58
|
2天前
|
JavaScript 开发工具
vite如何打包vue3插件为JSSDK
【9月更文挑战第10天】以下是使用 Vite 打包 Vue 3 插件为 JS SDK 的步骤:首先通过 `npm init vite-plugin-sdk --template vue` 创建 Vue 3 项目并进入项目目录 `cd vite-plugin-sdk`。接着,在 `src` 目录下创建插件文件(如 `myPlugin.js`),并在 `main.js` 中引入和使用该插件。然后,修改 `vite.config.js` 文件以配置打包选项。最后,运行 `npm run build` 进行打包,生成的 `my-plugin-sdk.js` 即为 JS SDK,可在其他项目中引入使用。
|
3天前
|
JavaScript 开发者
彻底搞懂 Vue3 中 watch 和 watchEffect是用法
彻底搞懂 Vue3 中 watch 和 watchEffect是用法
|
9天前
|
JavaScript API
如何使用Vue3的可计算属性
【9月更文挑战第5天】如何使用Vue3的可计算属性
42 13
vue3 reactive数据更新,视图不更新问题
vue3 reactive数据更新,视图不更新问题
|
9天前
|
资源调度 JavaScript API
vue3 组件通信方式
vue3 组件通信方式
39 11
|
1天前
|
JavaScript
|
1天前
vue3定义暴露一些常量
vue3定义暴露一些常量