手写一个数字动态滚动加载组件,从0加载到指定数字

新建组件CountUp.vue

javascript 复制代码
<template>
    <div>{{ thousandthPercentile?count.toLocaleString('en-US'):count }}</div>
</template>

<script>
export default {
    name: "CountUp",
    props: {
        start: {
            type: Number,
            default: 0,//开始值
        },
        end: {
            type: Number,
            default: 100,//结束值
        },
        stepTime: {
            type: Number,
            default: 10,//切换时间间隔
        },
        duration: {
            type: Number,
            default: 2000,//持续时间
        },
        thousandthPercentile: {
            type: Boolean,
            default: false,//是否使用千位分隔符
        }
    },
    data() {
        return {
            count: this.start,
            timer: null,
        };
    },
    watch:{
        end:{
            handler(val){
                // console.log('目标值',val)
                this.animateCountUp()
            }
        },
    },
    mounted() {
        // console.log('目标值',this.end)
        this.animateCountUp();
    },
    methods: {
        animateCountUp() {
            // console.log('参数',this.end,this.duration)
            if(this.timer){
                clearInterval(this.timer);
            }
            const start = this.start;//起始值
            const end = this.end;//结束值
            const duration = this.duration;//持续时间间隔
            const stepTime = this.stepTime;//切换时间
            const range = end - start;
            let current = start;
            // let increment = parseInt(range / (duration / stepTime));
            let increment = range / (duration / stepTime);
            this.timer = setInterval(() => {
                current += increment;
                // console.log('加载',this.count,range,duration,stepTime,increment)
                if (current >= end) {
                    this.count = end;
                    clearInterval(this.timer);
                } else {
                    this.count = parseInt(current);
                }
            }, stepTime);
        },
    },
};
</script>

使用该组件

javascript 复制代码
<template>
<CountUp class="item_count" :end="count" :thousandthPercentile="true"></CountUp>
</template>
<script>
import CountUp from "./CountUp"
export default {
    name: "ActualInformation",
    components:{ CountUp },
    data(){
        return{
			count:9999
		}
    }
}
</script>
<style>
.item_count{
   font-family: "ChePaiZiTi";
   font-size: 26px;
   font-weight: bold;
   text-align: left;
   color: #00FFAA;
   margin-right: 3px;
}
</style>
相关推荐
1024小神3 分钟前
tauri项目在windows上的c盘没有权限写入文件
c语言·开发语言·windows
KaneLogger5 分钟前
视频转文字,别再反复拖进度条了
前端·javascript·人工智能
老虎062711 分钟前
数据结构(Java)--位运算
java·开发语言·数据结构
yanjiaweiya11 分钟前
云原生-集群管理续
java·开发语言·云原生
Swift社区12 分钟前
Swift 解 LeetCode 320:一行单词有多少种缩写可能?用回溯找全解
开发语言·leetcode·swift
写不出来就跑路17 分钟前
暑期实习感悟与经验分享:从校园到职场的成长之路
java·开发语言·经验分享·spring boot
前端风云志18 分钟前
JavaScript中如何遍历对象?
javascript
weixin_472339466 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
zwjapple6 小时前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
枯萎穿心攻击6 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎