js数字变动效果

1.定时器

        //数字变化效果
        numberChange() {
            let timerId;
            // 清除之前的定时器,以防重复启动
            if (timerId) {
                clearInterval(timerId);
            }
            // 设置新的定时器
            timerId = setInterval(() => {
                this.animation(970, 10, 97, (val) => {
                    //this.count要展示的数字
                    this.count = val.toFixed(0);
                });
            }, 5000); // 每0.1秒执行一次
        },

2.设置数字变动

animation(duration, from, to, onProgress) {
            let value = from;
            const start = Date.now();
            // 变化速度
            const speed = (to - from) / duration;
            // _run函数:让value一点一点变化
            function _run() {
                // 1、改变value的值
                const t = Date.now() - start;
                if (t >= duration) {
                    value = to;
                    onProgress(value);
                    return;
                }
                value = from + t * speed;
                onProgress(value);
                // 2、注册下一次的变化
                requestAnimationFrame(_run);
            }
            // 一开始执行
            _run();
        },

3.效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值