template如图所示:
<template>
<div class="chartNum">
<div class="box-item">
<li
:class="{ 'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
v-for="(item, index) in orderNum"
:key="index"
>
<span v-if="!isNaN(item)">
<i ref="numberItem">0123456789</i>
</span>
<span class="comma" v-else>{{ item }}</span>
</li>
</div>
</div>
</template>
scrpit如图所示:
<script>
export default {
props: {
// 显示的数字
number: {
type: Number,
},
// 显示的长度
length: {
type: Number,
},
orderNums: {
type: Array,
}
},
data() {
return {
orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'], // 默认总数
};
},
mounted() {
setTimeout(() => {
this.toOrderNum(this.number); // 这里输入数字即可调用
}, 500);
},
watch: {
number: {
handler(val) {
this.toOrderNum(val);
},
deep: true,
},
orderNums(val) {
console.log('输入数组:' + val);
this.orderNum = val;
}
},
methods: {
// 设置文字滚动
setNumberTransform() {
const numberItems = this.$refs.numberItem; // 拿到数字的ref,计算元素数量
// eslint-disable-next-line no-restricted-globals
const numberArr = this.orderNum.filter(item => !isNaN(item));
console.log(numberItems.length, numberArr);
// 结合CSS 对数字字符进行滚动,显示数量
for (let index = 0; index < numberItems.length; index += 1) {
const elem = numberItems[index];
elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`;
}
},
// 处理总数字
toOrderNum(num) {
console.log(num);
const numtext = num.toString();
if (this.length) {
if (numtext.length < this.length) {
const numlist = `0${numtext}`; // 如未满固定数,添加"0"补位
this.toOrderNum(numlist); // 递归添加"0"补位
} else if (numtext.length === num.length) {
this.orderNum = numtext.split(''); // 将其便变成数据,渲染至滚动数组
}
} else {
this.orderNum = numtext.split('');
}
// 数字中加入逗号
// const length = numtext.length / 3;
// let count = '';
// for (let i = 0; i < length; i += 1) {
// if (i === 0) {
// count += `${numtext.slice(i, i + 3)},`;
// console.log(count);
// } else if (i === length - 1) {
// count += numtext.slice(i * 3, i * 3 + 3);
// console.log(count);
// } else {
// count += `${numtext.slice(i * 3, i * 3 + 3)},`;
// console.log(count);
// }
// }
// this.orderNum = count.split('');
this.setNumberTransform();
},
},
};
</script>
CSS如图所示:(注意!【lang】是【scss】)
<style scoped lang="scss">
/*总量滚动数字设置*/
.box-item {
position: relative;
height: 34px;
font-size: 20px;
font-family: AzonixRegular;
color: #021c25;
line-height: 41px;
text-align: center;
list-style: none;
writing-mode: vertical-lr;
text-orientation: upright;
}
/* 默认逗号设置 */
.mark-item {
width: 28px;
height: 34px;
position: relative;
/* 背景图片 */
background: url('../img/pai.png') no-repeat center center;
background-size: 100% 100%;
list-style: none;
margin-right: 1px;
&>span {
position: absolute;
width: 100%;
height: 100%;
bottom: 2px;
left: 20%;
font-size: 20px;
writing-mode: vertical-rl;
text-orientation: upright;
}
}
/*滚动数字设置*/
.number-item {
width: 28px;
height: 34px;
/* 背景图片 */
background: url('../img/pai.png') no-repeat center center;
background-size: 100% 100%;
// background: #ccc;
list-style: none;
margin-right: 1px;
&>span {
position: relative;
display: inline-block;
margin-right: 10px;
width: 100%;
height: 100%;
writing-mode: vertical-rl;
text-orientation: upright;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
&>i {
font-style: normal;
position: absolute;
top: 8px;
left: 50%;
transform: translate(-50%, 0);
transition: transform 1s ease-in-out;
letter-spacing: 10px;
}
}
}
.number-item:last-child {
margin-right: 0;
}
</style>
(注:原版的好像会因为切换的太快而报错,我没有对源代码做修改,而是重新加入了一个传参:orderNums 直接赋值给了orderNum)
(注2:这个方法因为我的调整,出现了第一次切换数字只切换位数,第二次才显示第一次的结果的【重大BUG】。有兴趣的可以自己动手试着改改。)
效果如图:



本文介绍了一个使用Vue.js实现的数字翻牌器,通过展示模板、脚本和SCSS代码来阐述实现过程。注意到原版可能存在因切换速度过快导致的错误,作者通过传入orderNums参数来避免这个问题。然而,调整后的代码存在一个bug,即首次切换只改变位数,第二次切换才会显示第一次的结果,鼓励读者尝试修复这一问题。
1万+

被折叠的 条评论
为什么被折叠?



