数字加载效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数字加载</title>
    <style>
        [v-cloak]{
            display: none;
        }
    </style>
</head>
<body>
    <div id="app" v-cloak>
        <div>{{num1}}</div>
        <div>{{num2}}</div>
        <div>{{num3}}</div>
        <button @click="getData">加载</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                num1: 0,  // 加载中的值
                num2: 0,
                num3: 0,
                s: null, // Set数据
            },
            created() {
                this.s = new Set()
                this.getData()
            },
            methods: {
                numberLoading(newNum, oldNum, name) {
                    if (newNum === oldNum) return
                    const gap = Math.abs(newNum - oldNum)
                    const step = Math.ceil(gap / 16)
                    let timer = setInterval(() => {
                        if (newNum > oldNum) {
                            this[name] += step
                            if (this[name] > newNum) {
                                this[name] = newNum
                                clearInterval(timer)
                            }
                        } else {
                            this[name] -= step
                            if (this[name] < newNum) {
                                this[name] = newNum
                                clearInterval(timer)
                            }
                        }
                    }, 16)
                    // 把定时器存放set数据中,防止上次定时器执行未完成,及时清除
                    this.s.add(timer)
                },
                getData() {
                    setTimeout(() => {
                        const num1 = Math.floor(Math.random()*100000)
                        const num2 = Math.floor(Math.random()*1000)
                        const num3 = Math.floor(Math.random()*30)
                        console.log(num1, num2, num3);
                        // 把定时器存放set数据中,防止上次定时器执行未完成,及时清除
                        for(const key of this.s) {
                            clearInterval(key)
                        }
                        this.s.clear()
                        this.numberLoading(num1, this.num1, 'num1')
                        this.numberLoading(num2, this.num2, 'num2')
                        this.numberLoading(num3, this.num3, 'num3')
                    }, 300)
                }
            }
        })
    </script>
</body>
</html>

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值