引入使用
import Number from './components/Number'
<Number name="今日省内监测" :number="996"></Number>
组件代码(感叹号最需要部分)
<template>
<div class="numDiv">
<table>{{ name }}</table>
<div class="chartNum">
<li v-for="(item, index) in orderNum" :key="index"
:class="{ 'number-item': !isNaN(item), 'mark-item': isNaN(item) }">
<span v-if="!isNaN(item)">
<i ref="numberItem">0123456789</i>
</span>
<span v-else class="comma">{{ item }}</span>
</li>
</div>
</div>
</template>
<script>
export default {
props: {
name: {
type: [Array, Object, Number, String],
default: null
},
number: {
type: [Array, Object, Number, String],
default: null
}
},
data() {
return {
orderNum: [0, 0, 0, 0] // 当前要渲染的元素长度
};
},
watch: {
number: {
handler: function (val) {
this.toOrderNum(val);
},
immediate: true // 首次赋值是否触发,一般不写
}
},
mounted() {
},
methods: {
// 设置文字滚动
setNumberTransform() {
const numberItems = this.$refs.numberItem; // 拿到数字的ref,计算元素数量
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];
let n = numberArr[index] * 10 <= 0 ? 0.5 : '-' + (numberArr[index] * 10 - 0.5) // 大小改变需要改变Y轴值保持居中(数字之间是靠字行高隔开的)
elem.style.transform = `translate(-50%, ${n}%)`;
}
// this.$nextTick(() => {
// this.$forceUpdate()
// })
},
// 处理总数字
toOrderNum(num) {
if (this.orderNum.length == 0) {
for (let i = 0; i <= String(num).length; i++) {
this.orderNum.push('0')
}
}
console.log(this.orderNum)
this.$nextTick(() => {
const numtext = num.toString()
// console.log(numtext, 4)
// if (this.length) {
if (numtext.length < 4) { // 最少4位数补齐0
const numlist = `0${numtext}` // 如未满固定数,添加"0"补位
this.toOrderNum(numlist) // 递归添加"0"补位
} else if (numtext.length === num.length) {
this.orderNum = numtext.split('') // 将其便变成数据,渲染至滚动数组
}
// } else {
// this.orderNum = numtext.split('')
// // console.log(this.orderNum)
// }
// 数字中加入逗号
// 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.$nextTick(() => {
setTimeout(() => {
this.setNumberTransform()
}, 500)
})
})
}
}
};
</script>
<style scoped lang="scss">
.numDiv {
flex: 1;
height: 58px;
text-align: center;
display: flex;
justify-content: center; //左右居中
>table {
float: left; //浮动
height: 100%;
line-height: 54px; //!
font-size: 20px;
margin-right: 20px;
}
.chartNum {
float: left; //浮动
position: relative;
height: 100%;
font-family: AzonixRegular;
color: #021c25;
text-align: center;
list-style: none;
writing-mode: vertical-lr;
text-orientation: upright;
/*滚动数字设置*/
.number-item {
width: 40px; //!
height: 100%;
/* 背景图片 */
margin-right: 14px; //!
border: 1px solid #688BB9;
background-image: radial-gradient(circle at center, rgba(118, 161, 222, 0.75), rgba(9, 41, 94, 0.75));
background-image: radial-gradient(center, ellipse cover, rgba(118, 161, 222, 0.75) 30%, rgba(9, 41, 94, 0.75) 100%);
background-image: radial-gradient(ellipse at center, rgba(118, 161, 222, 0.75) 30%, rgba(9, 41, 94, 0.75) 100%);
>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-size: 30px; //!
font-style: normal;
position: absolute;
top: 8px;
left: 50%;
transform: translate(-50%, 0.5%); //大小改变需要改变Y轴值保持居中(数字之间是靠字行高隔开的)!
transition: transform 1s ease-in-out;
letter-spacing: 58px; //数字行高!
color: #fff;
}
}
}
.number-item:last-child {
margin-right: 0;
}
/* 默认逗号设置 */
.mark-item {
width: 28px;
height: 100%;
position: relative;
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;
}
}
}
}
</style>
网友评论