Vue3数值动画(NumberAnimation)

73 篇文章 4 订阅
70 篇文章 3 订阅

效果如下图:在线预览

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

APIs

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

Methods

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

Events

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

安装依赖:

pnpm add @vueuse/core

其中引入使用了 formatNumber() 工具函数

创建数值动画组件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: '',
  suffix: '',
  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>
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3提供了一种简单而灵活的方式来实现加载动画。你可以使用Vue的transition组件和动画类来实现。 首先,你需要在Vue组件中使用transition组件来包裹需要添加动画的元素。transition组件可以根据元素的进入和离开状态来应用动画效果。 下面是一个简单的示例,展示了如何使用Vue 3的transition组件来实现加载动画: ```html <template> <div> <transition name="fade"> <div v-if="isLoading" class="loading"> <!-- 加载动画的内容 --> </div> </transition> <button @click="startLoading">开始加载</button> </div> </template> <script> export default { data() { return { isLoading: false }; }, methods: { startLoading() { this.isLoading = true; // 模拟加载过程 setTimeout(() => { this.isLoading = false; }, 2000); } } }; </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter, .fade-leave-to { opacity: 0; } .loading { /* 加载动画的样式 */ } </style> ``` 在上面的示例中,我们使用了Vue的transition组件,并给它设置了一个name属性为"fade"。然后,在需要添加加载动画的地方,我们使用v-if指令来控制元素的显示和隐藏。当isLoading为true时,元素会以淡入的动画效果显示出来;当isLoading为false时,元素会以淡出的动画效果隐藏起来。 你可以根据自己的需求,自定义transition组件的样式和动画效果。在上面的示例中,我们使用了fade-enter-active、fade-leave-active、fade-enter和fade-leave-to这几个类来定义了一个简单的淡入淡出效果。 希望这个示例能够帮助你实现Vue 3的加载动画。如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值