当前位置: 首页 > news >正文

vue3封装数值动态递增组件

vue3封装数值动态递增组件

  • 前言
  • 源码
  • 举个例子:


前言

1)使用技术:

vue3.2 + Ts

2)组件接收参数:

参数类型意义是否可选
valuenumber数值大小必填
durationnumber递增动画持续时间(单位:s)可选,默认为2
isDecimalboolean是否显示为小数可选,默认为false

3)补充:
组件本身没有过多样式,想实现不同样式可以在调用组件时自定义设置样式

源码

<template><div><span ref="numberDom">0</span></div>
</template><script setup lang="ts">
import { ref, onMounted, onBeforeUpdate, onBeforeUnmount, withDefaults, defineProps } from 'vue';/*** @param value 数值大小 * @param duration 递增动画持续时间;* @param isDecimal 是否显示为小数*/
const props = withDefaults(defineProps<{value: number,duration: number,isDecimal: boolean}>(), {duration: 2,isDecimal: false
})let timer: number  | null = null
const timerDelay = 5
const numberDom = ref<any>(null)onMounted(() => {numericalIncrement(numberDom.value)
})
onBeforeUpdate(() => {if (timer) {clearInterval(timer!)timer = null}numericalIncrement(numberDom.value)
})
onBeforeUnmount(() => {if (timer) {clearInterval(timer!)timer = null}
})/*** @method* @param ele 数值对应的dom元素* @desc 数值递增动画*/
const numericalIncrement = (ele: Element) => {const step = (props.value * timerDelay) / (props.duration * 1000)let current: number = 0let start: number = 0let flag: boolean = falsetimer = setInterval(() => {start += stepif (start >= props.value) {flag = props.isDecimalclearInterval(timer!)start = props.valuetimer = null}current = startif (flag) {ele.innerHTML = current.toString().replace(/(\d)(?=(?:\d{3}[+]?)+$)/g, '$1,')} else {ele.innerHTML = current.toFixed(0) .toString().replace(/(\d)(?=(?:\d{3}[+]?)+$)/g, '$1,')}}, timerDelay)
}
</script><style scoped>
div {display: inline-block;
}
</style>

举个例子:

1)使用代码

<template><div><NumericalIncrement :duration="2" :is-decimal="true" :value="val" class="num"></NumericalIncrement></div>
</template><script setup lang="ts">
import NumericalIncrement from './components/NumericalIncrement.vue'
import {ref,onMounted} from 'vue';
const val = ref(110)
onMounted(()=>{setTimeout(()=>{val.value=200},3000)
})
</script><style scoped>
.num {min-width: 40px;text-align: center;font-size: 20px;background-color: orange;color:#fff;
}
</style>

2)效果在这里插入图片描述


提示:文章到此结束,文章为个人学习记录,侵删。
http://www.lryc.cn/news/6294.html

相关文章:

  • JavaWeb_RequestResponse
  • C语言刷题——“C”
  • 【刷题】搜索——BFS:城堡问题(The Castle)
  • 深度学习——torch相关函数用法解析
  • ubuntu 20使用kubeadm安装k8s 1.26
  • 低代码开发平台|制造管理-生产过程管理搭建指南
  • python对多个csv文件进行合并(表头需一致)
  • Salesforce Apex调用邮件模板
  • windows本地开发Spark[不开虚拟机]
  • 一文教你快速估计个股交易成本
  • Leetcode—移除元素、删除有序数组中的重复项、合并两个有序数组
  • 面试(十)大疆 安全开发 C++1面
  • 短信链接跳转微信小程序
  • 吉林电视台启用乾元通多卡聚合系统广电视频传输解决方案
  • Linux常用命令1
  • 【C++进阶】一、继承(总)
  • AttributeError: module ‘lib‘ has no attribute ‘OpenSSL_add_all_algorithms
  • Python实现视频自动打码功能,避免看到羞羞的画面
  • 说说Knife4j
  • Java学习笔记-03(API阶段-2)集合
  • 「3」线性代数(期末复习)
  • 【CSDN竞赛】27期题解(Javascript)
  • 高压放大器在骨的逆力电研究中的应用
  • 思科网络部署,(0基础)入门实验,超详细
  • private static final Long serialVersionUID= 1L详解
  • 若依前后端分离版集成nacos
  • JAVA面试八股文一(mysql)
  • 动静态库概念及创建
  • 【H.264】码流解析 annexb vs avcc
  • 【最优化方法】1-最优化方法介绍