当前位置:首页 > VUE

vue 实现数字跳动

2026-01-16 06:12:29VUE

Vue 实现数字跳动效果

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

使用 Vue Transition 和 CSS 动画

通过 Vue 的 <transition> 组件结合 CSS 动画实现数字平滑过渡。

<template>
  <div>
    <transition name="slide-up" mode="out-in">
      <span :key="currentNumber">{{ currentNumber }}</span>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentNumber: 0,
      targetNumber: 100
    };
  },
  mounted() {
    this.animateNumber();
  },
  methods: {
    animateNumber() {
      const duration = 2000; // 动画持续时间(毫秒)
      const startTime = Date.now();
      const startValue = this.currentNumber;

      const updateNumber = () => {
        const elapsed = Date.now() - startTime;
        const progress = Math.min(elapsed / duration, 1);
        this.currentNumber = Math.floor(startValue + (this.targetNumber - startValue) * progress);

        if (progress < 1) {
          requestAnimationFrame(updateNumber);
        }
      };

      requestAnimationFrame(updateNumber);
    }
  }
};
</script>

<style>
.slide-up-enter-active, .slide-up-leave-active {
  transition: all 0.5s ease;
}
.slide-up-enter-from, .slide-up-leave-to {
  opacity: 0;
  transform: translateY(20px);
}
</style>

使用 GSAP 动画库

GSAP 提供了更强大的动画控制能力,适合复杂数字动画效果。

<template>
  <div>
    <span ref="numberElement">{{ currentNumber }}</span>
  </div>
</template>

<script>
import { gsap } from 'gsap';

export default {
  data() {
    return {
      currentNumber: 0
    };
  },
  mounted() {
    this.animateWithGSAP();
  },
  methods: {
    animateWithGSAP() {
      gsap.to(this, {
        duration: 2,
        currentNumber: 100,
        onUpdate: () => {
          this.currentNumber = Math.floor(this.currentNumber);
        }
      });
    }
  }
};
</script>

使用 vue-count-to 组件

专门为 Vue 设计的数字动画组件,简单易用。

安装:

npm install vue-count-to

使用:

<template>
  <countTo :startVal="0" :endVal="100" :duration="2000"></countTo>
</template>

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

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

自定义缓动函数

实现更自然的数字跳动效果,可以添加缓动函数。

easeOutQuad: function(t, b, c, d) {
  return -c * (t /= d) * (t - 2) + b;
}

在动画更新时调用缓动函数计算当前值:

vue 实现数字跳动

this.currentNumber = Math.floor(this.easeOutQuad(progress, startValue, endValue - startValue, 1));

以上方法可根据需求选择,Vue Transition 适合简单效果,GSAP 适合复杂动画,vue-count-to 则提供了开箱即用的解决方案。

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

相关文章

vue前端实现注册

vue前端实现注册

使用 Vue 实现用户注册功能 环境准备 确保已安装 Vue CLI 或通过 CDN 引入 Vue。若使用 Vue CLI,需安装 axios 或其他 HTTP 库用于后端通信。 注册表单组件 创建…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…