当前位置:首页 > VUE

vue实现123456

2026-03-07 08:13:07VUE

Vue 实现数字递增动画

在 Vue 中实现数字从 1 递增到 123456 的动画效果,可以通过以下方法实现:

方法一:使用 transition 和计算属性

<template>
  <div>
    <transition name="count-up">
      <span>{{ animatedNumber }}</span>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      targetNumber: 123456,
      currentNumber: 0
    }
  },
  computed: {
    animatedNumber() {
      return Math.floor(this.currentNumber)
    }
  },
  mounted() {
    const duration = 2000 // 动画持续时间(毫秒)
    const startTime = performance.now()

    const animate = (timestamp) => {
      const elapsed = timestamp - startTime
      const progress = Math.min(elapsed / duration, 1)
      this.currentNumber = progress * this.targetNumber

      if (progress < 1) {
        requestAnimationFrame(animate)
      }
    }

    requestAnimationFrame(animate)
  }
}
</script>

<style>
.count-up-enter-active, .count-up-leave-active {
  transition: all 1s ease;
}
</style>

方法二:使用第三方库 vue-countup

安装依赖:

vue实现123456

npm install vue-countup

实现代码:

<template>
  <count-up 
    :startVal="0" 
    :endVal="123456" 
    :duration="2"
  />
</template>

<script>
import CountUp from 'vue-countup'

export default {
  components: { CountUp }
}
</script>

方法三:纯 CSS 动画(适用于简单场景)

vue实现123456

<template>
  <div class="count-up">{{ animatedNumber }}</div>
</template>

<script>
export default {
  data() {
    return {
      animatedNumber: 0
    }
  },
  mounted() {
    const increment = 123456 / 60 // 假设60帧完成
    const interval = setInterval(() => {
      this.animatedNumber += increment
      if (this.animatedNumber >= 123456) {
        this.animatedNumber = 123456
        clearInterval(interval)
      }
    }, 16) // 约60fps
  }
}
</script>

<style>
.count-up {
  font-size: 24px;
  font-weight: bold;
  transition: all 2s ease-out;
}
</style>

性能优化建议

对于大数字动画,建议使用 requestAnimationFrame 而不是 setInterval,因为它能提供更平滑的动画效果并减少性能消耗。

可以添加缓动函数使动画更自然:

function easeOutQuad(t) {
  return t * (2 - t)
}

然后在动画计算中使用:

this.currentNumber = easeOutQuad(progress) * this.targetNumber

标签: vue
分享给朋友:

相关文章

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…