当前位置:首页 > VUE

vue实现数字跳动

2026-03-09 06:02:35VUE

实现数字跳动效果

使用Vue实现数字跳动效果可以通过过渡动画或第三方库完成。以下是几种常见方法:

使用Vue过渡动画

通过Vue的过渡系统结合CSS动画实现基础数字跳动:

<template>
  <div>
    <transition name="bounce">
      <span :key="currentNumber">{{ currentNumber }}</span>
    </transition>
    <button @click="increment">增加</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentNumber: 0
    }
  },
  methods: {
    increment() {
      this.currentNumber += 1
    }
  }
}
</script>

<style>
.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% {
    transform: scale(0.8);
    opacity: 0;
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
</style>

使用Tween.js实现平滑过渡

通过Tween.js库实现数字平滑过渡动画:

<template>
  <div>
    <span>{{ animatedNumber.toFixed(0) }}</span>
    <button @click="animateTo(100)">跳动到100</button>
  </div>
</template>

<script>
import TWEEN from '@tweenjs/tween.js'

export default {
  data() {
    return {
      animatedNumber: 0
    }
  },
  mounted() {
    this.animate()
  },
  methods: {
    animateTo(newValue) {
      new TWEEN.Tween({ number: this.animatedNumber })
        .to({ number: newValue }, 1000)
        .easing(TWEEN.Easing.Quadratic.Out)
        .onUpdate(obj => {
          this.animatedNumber = obj.number
        })
        .start()
    },
    animate() {
      requestAnimationFrame(this.animate)
      TWEEN.update()
    }
  }
}
</script>

使用vue-count-to插件

专门为Vue设计的数字动画组件:

<template>
  <div>
    <countTo :startVal="0" :endVal="targetValue" :duration="2000"></countTo>
    <button @click="targetValue += 100">增加100</button>
  </div>
</template>

<script>
import countTo from 'vue-count-to'

export default {
  components: { countTo },
  data() {
    return {
      targetValue: 0
    }
  }
}
</script>

自定义数字跳动组件

创建可复用的数字跳动组件:

<template>
  <span>{{ displayValue }}</span>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      required: true
    },
    duration: {
      type: Number,
      default: 1000
    }
  },
  data() {
    return {
      displayValue: 0,
      startTime: null,
      startValue: 0
    }
  },
  watch: {
    value(newVal) {
      this.startAnimation(newVal)
    }
  },
  mounted() {
    this.displayValue = this.value
  },
  methods: {
    startAnimation(endValue) {
      this.startValue = this.displayValue
      this.startTime = null
      const step = timestamp => {
        if (!this.startTime) this.startTime = timestamp
        const progress = Math.min((timestamp - this.startTime) / this.duration, 1)
        this.displayValue = Math.floor(this.startValue + (endValue - this.startValue) * progress)
        if (progress < 1) {
          requestAnimationFrame(step)
        }
      }
      requestAnimationFrame(step)
    }
  }
}
</script>

实现数字格式化

在数字跳动过程中添加格式化功能:

vue实现数字跳动

// 在组件方法中添加
formatNumber(num) {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}

以上方法可根据实际需求选择使用,从简单CSS动画到复杂的数字过渡效果均可实现。

标签: 数字vue
分享给朋友:

相关文章

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="cloc…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue nexttick 实现

vue nexttick 实现

Vue nextTick 实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它主要用于确保在 DOM 更新完成后执行某些操作。 核心实现机制 ne…