手写一个数字动态滚动加载组件,从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>
相关推荐
天***88961 小时前
js封装一个双精度算法实现
开发语言·前端·javascript
.小小陈.1 小时前
数据结构2:单链表
c语言·开发语言·数据结构·笔记·学习方法
Camel卡蒙1 小时前
数据结构——二叉搜索树Binary Search Tree(介绍、Java实现增删查改、中序遍历等)
java·开发语言·数据结构
Algebraaaaa1 小时前
什么是前端、后端与全栈开发,Qt属于什么?
开发语言·前端·qt
立志成为大牛的小牛1 小时前
数据结构——二十三、并查集的终极优化(王道408)
开发语言·数据结构·笔记·学习·程序人生·考研
胡斌附体1 小时前
使用Electron创建helloworld程序
前端·javascript·electron·nodejs·pc
toobeloong1 小时前
Electron 从低版本升级到高版本 - webview通信的改造
前端·javascript·electron
im_AMBER2 小时前
React 01
前端·javascript·笔记·react.js·前端框架·web
@大迁世界2 小时前
React 19.2.0 有哪些新变化
前端·javascript·react.js·前端框架·ecmascript