vue3封裝數(shù)字滾動組件的實現(xiàn)示例
更新時間:2024年08月02日 10:14:11 作者:月傷59
本文主要介紹了vue3封裝數(shù)字滾動組件的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
效果展示:

代碼:
<template>
<div class="counter-container">
<div v-for="(item, index) in numArr" :key="index" :class="item === ',' || item === '.' ? 'mark-item' : 'num-item'">
<span class="pointer" v-if="item == ',' || item == '.'">{{ item }}</span>
<span ref="numberItem" v-else class="num">
{{ flipNum }}
</span>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, watch, nextTick, onMounted } from "vue";
const props = defineProps({
num: {
type: Number,
default: 0
},
duration: {
type: Number,
default: 1
}
});
let numArr = ref(["0", "0", ",", "0", "0", "0", ",", "0", "0", "0"]);
const flipNum = "0123456789";
// let timer = null;
const numberItem = ref<Array<HTMLSpanElement> | null>(null);
// 處理傳遞過來的數(shù)字,轉(zhuǎn)化為數(shù)組
function numToArr() {
const str = props.num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
numArr.value = str.split("");
}
function rollNum() {
const numItems = numberItem.value;
const numberArr = numArr.value.filter(item => item !== "," && item !== ".");
numberArr.forEach((item, index) => {
const num = parseInt(item);
if (numItems) {
const numItem = numItems[index];
numItem.style.transform = `translate(-50%, -${num * 10}%)`;
}
});
}
// 監(jiān)聽數(shù)字變化,更新數(shù)字
const initNumber = () => {
numToArr();
nextTick(() => {
setTimeout(() => {
rollNum();
}, props.duration * 1000);
});
};
watch(
() => props.num,
() => {
initNumber();
}
);
onMounted(() => {
initNumber();
});
</script>
<style lang="scss" scoped>
.counter-container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.counter-container .num-item {
position: relative;
box-sizing: border-box;
width: 22px;
height: 34px;
padding: 8px;
margin-left: 4px;
overflow: hidden;
font-size: 20px;
font-weight: bold;
color: #ffffff;
text-align: center;
background-image: url("@/assets/images/dataBoard/num_wrap.png");
background-repeat: no-repeat;
background-size: 100% 100%;
}
.counter-container .mark-item {
height: 34px;
margin-left: 4px;
font-size: 24px;
font-weight: 800;
line-height: 38px;
color: #ffffff;
text-align: center;
}
.counter-container .num {
position: absolute;
top: 6px;
left: 50%;
letter-spacing: 8px;
writing-mode: vertical-lr;
transition: all 1s ease-in-out;
transform: translate(-50%, 0);
text-orientation: upright;
}
</style>到此這篇關(guān)于vue3封裝數(shù)字滾動組件的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue3 數(shù)字滾動組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.0搭配.net core實現(xiàn)文件上傳組件
這篇文章主要介紹了vue3.0搭配.net core實現(xiàn)文件上傳組件,幫助大家開發(fā)Web應(yīng)用程序,完成需求,感興趣的朋友可以了解下2020-10-10
Vue Router的三種歷史模式應(yīng)用與對比詳解
在 Vue.js 單頁應(yīng)用(SPA)中,vue-router 提供了三種不同的 歷史模式 來管理路由導(dǎo)航和 URL 顯示方式,每種模式適用于不同場景,下面小編就和大家簡單介紹一下吧2025-10-10

