当前位置:首页 > VUE

vue实现数字动态滚动

2026-02-22 15:42:04VUE

实现数字动态滚动的方案

在Vue中实现数字动态滚动效果,可以通过多种方式实现。以下是几种常见的方法:

使用CSS动画

通过CSS的transitionanimation属性实现数字变化时的平滑过渡效果。这种方法简单但控制粒度较粗。

<template>
  <div class="animated-number">{{ currentValue }}</div>
</template>

<script>
export default {
  data() {
    return {
      currentValue: 0,
      targetValue: 100
    }
  },
  mounted() {
    this.animateNumber()
  },
  methods: {
    animateNumber() {
      const duration = 2000 // 动画持续时间
      const startTime = performance.now()

      const updateValue = (timestamp) => {
        const elapsed = timestamp - startTime
        const progress = Math.min(elapsed / duration, 1)
        this.currentValue = Math.floor(progress * this.targetValue)

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

      requestAnimationFrame(updateValue)
    }
  }
}
</script>

<style>
.animated-number {
  transition: all 1s ease-out;
}
</style>

使用第三方库

Vue-count-to是一个专门用于数字动画的Vue组件,提供更多配置选项。

安装:

npm install vue-count-to

使用示例:

<template>
  <countTo :startVal="0" :endVal="1000" :duration="3000"></countTo>
</template>

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

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

自定义动画函数

对于更复杂的动画需求,可以创建自定义动画函数,结合requestAnimationFrame实现。

<template>
  <div>{{ animatedValue }}</div>
</template>

<script>
export default {
  data() {
    return {
      startValue: 0,
      endValue: 1000,
      duration: 2000,
      animatedValue: 0
    }
  },
  mounted() {
    this.startAnimation()
  },
  methods: {
    startAnimation() {
      let startTimestamp = null

      const step = (timestamp) => {
        if (!startTimestamp) startTimestamp = timestamp
        const progress = Math.min((timestamp - startTimestamp) / this.duration, 1)

        this.animatedValue = Math.floor(
          this.startValue + progress * (this.endValue - this.startValue)
        )

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

      requestAnimationFrame(step)
    }
  }
}
</script>

实现细节优化

缓动函数应用

vue实现数字动态滚动

为动画添加缓动效果可以使滚动更自然。常用的缓动函数包括easeInOutQuad、easeOutCubic等。

// 缓动函数示例
function easeOutCubic(t) {
  return 1 - Math.pow(1 - t, 3)
}

// 在动画函数中使用
const easedProgress = easeOutCubic(progress)
this.animatedValue = Math.floor(
  this.startValue + easedProgress * (this.endValue - this.startValue)
)

格式化显示

对于大数字,可以添加千位分隔符或货币符号等格式化显示。

methods: {
  formatNumber(value) {
    return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
  }
}

响应式更新

当目标值变化时自动触发新动画,可以添加watch监听。

vue实现数字动态滚动

watch: {
  endValue(newVal) {
    this.startValue = this.animatedValue
    this.endValue = newVal
    this.startAnimation()
  }
}

性能考虑

减少DOM操作

避免在动画过程中频繁操作DOM,使用虚拟DOM的特性让Vue批量处理更新。

使用will-change

对动画元素应用CSS will-change属性,提示浏览器优化。

.animated-number {
  will-change: contents;
}

适时停止动画

在组件销毁前取消未完成的动画帧请求。

beforeDestroy() {
  cancelAnimationFrame(this.animationFrameId)
}

以上方法可以根据具体需求选择或组合使用,实现从简单到复杂的数字滚动效果。

标签: 数字动态
分享给朋友:

相关文章

jquery 数字

jquery 数字

jQuery 数字操作 jQuery 本身不直接提供数字操作的方法,但可以结合 JavaScript 的数字处理功能来实现。以下是常见的数字操作场景及实现方式。 数字格式化 使用 JavaScrip…

vue实现数字加减

vue实现数字加减

Vue 实现数字加减功能 在 Vue 中实现数字加减功能可以通过数据绑定、方法调用和事件处理来实现。以下是几种常见的实现方式: 使用 data 和 methods 在 Vue 实例中定义一个数字变量…

vue实现动态弧线

vue实现动态弧线

Vue 实现动态弧线的方法 动态弧线可以通过 SVG 或 Canvas 实现,以下是两种常见的方法: 使用 SVG 实现动态弧线 SVG 的 <path> 元素可以绘制弧线,结合…

vue动态加载实现

vue动态加载实现

Vue动态加载实现方法 动态加载在Vue中通常指按需加载组件或资源,以下是几种常见实现方式: 使用异步组件 通过defineAsyncComponent或动态import()语法实现组件按需加载:…

vue 实现动态组件

vue 实现动态组件

Vue 动态组件的实现方法 在 Vue 中,可以通过 <component> 标签结合 is 属性实现动态组件切换。以下是具体实现方式: 使用 is 属性绑定组件名 <tem…

vue动态实现表

vue动态实现表

Vue 动态实现表格的方法 使用 v-for 动态渲染表格 通过 Vue 的 v-for 指令可以动态渲染表格行和列。定义一个数组作为数据源,在模板中使用 v-for 遍历数组生成表格内容。 &l…