js实现数字动态增长效果

149 阅读1分钟
data() {
    return {
        num: '',
        timer: null
    }
},
computed: {
    totalNum() {
        return this.num || '';
    },
},
methods:{
    numIncrease() {
        const total = 15093720.11;
        this.num = total - 100;
        let step=0.5;
        this.timer = setInterval(() => {
            step=Number((Math.random() *5).toFixed(2)) // 随机生成0~5的小数
            this.num = Number((this.num + step).toFixed(2));
            if (this.num >= total) {
                this.num = total;
                clearInterval(this.timer)
                this.timer = null
            }
        }, 50)
    }
}