vue实现数字金额动态变化效果

编程入门 行业动态 更新时间:2024-10-26 15:16:43

vue实现数字<a href=https://www.elefans.com/category/jswz/34/1746174.html style=金额动态变化效果"/>

vue实现数字金额动态变化效果

目录

      • 1 前言
      • 2 数字动态滚动
        • 2.1 计时器实现
        • 2.2 动画帧实现
      • 3 总结

1 前言

  在某些场景中,要求我们能够动态与用户进行交互,如页面加载一个数字的时候,动态改变一个固定地数字,然后将数字展现在页面上,如当前的积分或者余额等;以达到提高与用户交互体验的效果。下面我们就在vue中使用两种方法来实现数字动态滚动效果。

2 数字动态滚动

2.1 计时器实现

先上效果:

  以上效果是点击按钮加载金额,实际情况是页面加载的时候就需要。这个时候我们需要在页面加载的时候执行用该方法。以上实例用到的代码如下:
代码:

<template><div><title-bar :title="title" @goBack="goback"></title-bar><div class="amount-box"><label>您的余额为:</label><label>{{ amountFormatPage }}</label></div><t-button @clickhandle="changeAmount" name="计时器"></t-button></div>
</template>
<script>
import TitleBar from "@/components/TitleBar";
import tButton from "@/components/TButton";
import { amountFormat } from "@/util/utils";
export default {components: {TitleBar,tButton},data() {return {res: false, //title: "动态变化金额",amount: 0, //初始amoutamoutFrame: 0,amountShow: 0};},computed: {// 计算属性格式化页面金额amountFormatPage() {return amountFormat(this.amount);}},methods: {changeAmount() {const total = 16000; //设置初始总金额const _this = this;let i = 0;//变化15次,每次间隔30毫秒const amoutInterval = setInterval(() => {if (i < 15) {i++;_this.amount = (total * i) / 15;} else {clearInterval(amoutInterval);}}, 30);},goback() {//}}
};
</script>
<style lang="scss" scoped>
.amount-box {label {display: box;}
}
</style>

除了这个效果以外,我们可以通过调节计时器的间隔时间来控制金额变化的速度;通过调节i的大小来控制变化的次数。主要是控制这两个参数来达到我们想要的效果。

2.2 动画帧实现

实现效果如下:

vue代码如下:

<template><div><title-bar :title="title" @goBack="goback"></title-bar><div><label>您的余额为:</label><label>{{ amountShow }}</label></div><t-button @clickhandle="changeAmountFrame" name="动画帧"></t-button></div>
</template>
<script>
import TitleBar from "@/components/TitleBar";
import tButton from "@/components/TButton";
import { amountFormat } from "@/util/utils";
export default {components: {TitleBar,tButton},data() {return {res: false, //title: "动态变化金额",amount: 0, //初始amoutamoutFrame: 0,amountShow: 0};},methods: {changeAmountFrame() {const total = 26666;const frameNum = 60;const _this = this;let animation = window.requestAnimationFrame(function f() {if (_this.amoutFrame < total) {_this.amoutFrame = _this.amoutFrame + total / frameNum;// 动画继续animation = window.requestAnimationFrame(f);} else {_this.amoutFrame = total;// 动画停止window.cancelAnimationFrame(f);}_this.amountShow = amountFormat(_this.amoutFrame);});},goback() {//}}
};
</script>
<style lang="scss" scoped>
.amount-box {label {display: box;}
}
</style>

window.requestAnimationFrame()是执行动画帧的关键方法,对应window.cancelAnimationFrame()是停止动画帧的方法。其中动画帧的价格时间一把为16ms,同上,通过调节framenum参数来控制一共播放多少帧,来达到我们想要的效果。

3 总结

  本次用两种方法实现金额数字等动态变化,偏向应用领域,若想了解即使器和动画帧的更深层次原理,请一部专业网站或者动手舱室一下。

更多推荐

vue实现数字金额动态变化效果

本文发布于:2024-02-11 23:39:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:金额   效果   数字   动态   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!