將1~9的數字豎直排版,經過translate移動位置顯示不一樣數字,使用transition控制transform屬性有動畫效果javascript
css 屬性 writing-mode: vertical-lr,使數字豎直排版css
移動 transform: translate(-50%, -40%); y值控制移動至哪一個數字html
transition 控制transform屬性有動畫效果java
<!--
* @Author: mat
* @Date: 2019-12-04 14:56:07
* @LastEditTime: 2019-12-04 16:31:52
* @LastEditors: Please set LastEditors
* @Description: 實現數字翻牌動態效果,css 屬性 writing-mode: vertical-lr,使數字豎直排版,
2d移動 transform: translate(-50%, -40%); y值控制移動至哪一個數字,transition
控制transform屬性有動畫效果
-->
<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>
<script>
export default {
data() {
return {
orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'], // 默認訂單總數
}
},
mounted(){
setTimeout(() => {
this.toOrderNum(12654) // 這裏輸入數字便可調用
}, 500);
},
methods: {
// 設置文字滾動
setNumberTransform () {
const numberItems = this.$refs.numberItem // 拿到數字的ref,計算元素數量
const numberArr = this.orderNum.filter(item => !isNaN(item))
// 結合CSS 對數字字符進行滾動,顯示訂單數量
for (let index = 0; index < numberItems.length; index++) {
const elem = numberItems[index]
elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
}
},
// 處理總訂單數字
toOrderNum(num) {
num = num.toString()
// 把訂單數變成字符串
if (num.length < 8) {
num = '0' + num // 如未滿八位數,添加"0"補位
this.toOrderNum(num) // 遞歸添加"0"補位
} else if (num.length === 8) {
// 訂單數中加入逗號
// num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)
this.orderNum = num.split('') // 將其便變成數據,渲染至滾動數組
} else {
// 訂單總量數字超過八位顯示異常
this.$message.warning('總量數字過大')
}
this.setNumberTransform()
},
}
}
</script>
<style scoped lang='scss'>
/*訂單總量滾動數字設置*/
.box-item {
position: relative;
height: 100px;
font-size: 54px;
line-height: 41px;
text-align: center;
list-style: none;
color: #2D7CFF;
writing-mode: vertical-lr;
text-orientation: upright;
/*文字禁止編輯*/
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit瀏覽器*/
-ms-user-select: none; /*IE10*/
-khtml-user-select: none; /*早期瀏覽器*/
user-select: none;
/* overflow: hidden; */
}
/* 默認逗號設置 */
.mark-item {
width: 10px;
height: 100px;
margin-right: 5px;
line-height: 10px;
font-size: 48px;
position: relative;
& > span {
position: absolute;
width: 100%;
bottom: 0;
writing-mode: vertical-rl;
text-orientation: upright;
}
}
/*滾動數字設置*/
.number-item {
width: 41px;
height: 75px;
/* 背景圖片 */
background: url(/images/text-bg-blue.png) no-repeat center center;
background-size: 100% 100%;
// background: #ccc;
list-style: none;
margin-right: 5px;
// background:rgba(250,250,250,1);
border-radius:4px;
border:1px solid rgba(221,221,221,1);
& > span {
position: relative;
display: inline-block;
margin-right: 10px;
width: 100%;
height: 100%;
writing-mode: vertical-rl;
text-orientation: upright;
overflow: hidden;
& > i {
font-style: normal;
position: absolute;
top: 11px;
left: 50%;
transform: translate(-50%,0);
transition: transform 1s ease-in-out;
letter-spacing: 10px;
}
}
}
.number-item:last-child {
margin-right: 0;
}
</style>
複製代碼