手写一个数字动态滚动加载组件,从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>
相关推荐
码上成长12 小时前
React 18 并发特性:useTransition 和 useDeferredValue 动画级解释
javascript·react.js·ecmascript
J***Q29212 小时前
前端CSS架构模式,BEM与ITCSS
前端·css
任子菲阳12 小时前
学Java第四十五天——斗地主小游戏创作
java·开发语言·windows
G***T69112 小时前
React性能优化实战,避免不必要的重渲染
前端·javascript·react.js
q***d17312 小时前
前端微前端部署方案,Nginx与Webpack
前端·nginx·webpack
y***548812 小时前
前端构建工具扩展,Webpack插件开发
前端·webpack·node.js
4***149012 小时前
前端构建工具多页面配置,Webpack与Vite
前端·webpack·node.js
缪懿13 小时前
JavaEE:多线程基础,多线程的创建和用法
java·开发语言·学习·java-ee
网络点点滴13 小时前
标签的ref属性
前端·javascript·vue.js
Boop_wu13 小时前
[Java EE] 多线程 -- 初阶(2)
java·开发语言·jvm