手写一个数字动态滚动加载组件,从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>
相关推荐
excel2 分钟前
前端必备:从能力检测到 UA-CH,浏览器客户端检测的完整指南
前端
前端小巷子9 分钟前
Vue 3全面提速剖析
前端·vue.js·面试
悟空聊架构16 分钟前
我的网站被攻击了,被干掉了 120G 流量,还在持续攻击中...
java·前端·架构
CodeSheep17 分钟前
国内 IT 公司时薪排行榜。
前端·后端·程序员
尖椒土豆sss21 分钟前
踩坑vue项目中使用 iframe 嵌套子系统无法登录,不报错问题!
前端·vue.js
遗悲风22 分钟前
html二次作业
前端·html
江城开朗的豌豆25 分钟前
React输入框优化:如何精准获取用户输入完成后的最终值?
前端·javascript·全栈
CF14年老兵26 分钟前
从卡顿到飞驰:我是如何用WebAssembly引爆React性能的
前端·react.js·trae
画月的亮29 分钟前
前端处理导出PDF。Vue导出pdf
前端·vue.js·pdf
江城开朗的豌豆35 分钟前
拆解Redux:从零手写一个状态管理器,彻底搞懂它的魔法!
前端·javascript·react.js