tui-digital-roller.vue
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
<view class="tui-digital__roller">
<view class="tui-digital__box" v-for="(item,index) in columns" :key="index"
:style="{width:cellWidth,height:height+'px'}">
<view class="tui-digital__column"
:style="{transform:`translate3d(0, -${keys[index] * height}px, 0)`,transitionDuration:`${duration}s`,transitionTimingFunction:animation}">
<view class="tui-digital__item" v-for="(val,idx) in item" :key="idx"
:style="{color:getColor,fontSize:size+'rpx',fontWeight:bold?'bold':'normal',height:height+'px',lineHeight:size+'rpx'}">
{{val}}</view>
</view>
</view>
</view>
</template>
<script>
var numArr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
export default {
name: 'tuiDigitalRoller',
props: {
value: {
type: [Number, String],
default: ''
},
color: {
type: String,
default: ''
},
//rpx
size: {
type: Number,
default: 32
},
bold: {
type: Boolean,
default: false
},
//滚动行高度 rpx
cellHeight: {
type: Number,
default: 32
},
//单个数字宽度
cellWidth: {
type: String,
default: 'auto'
},
// 动画过渡效果
animation: {
type: String,
default: 'cubic-bezier(0, 1, 0, 1)'
},
//动画时间
duration: {
type: Number,
default: 1.2
}
},
watch: {
value(val) {
this.initColumn(this.value)
},
cellHeight(val) {
this.handleHeight(val)
}
},
computed:{
getColor(){
return this.color || (uni && uni.$tui && uni.$tui.color.primary) || '#5677fc'
}
},
data() {
return {
columns: [],
keys: [],
height: 0
};
},
created() {
this.handleHeight(this.cellHeight)
this.initColumn(this.value)
},
methods: {
handleHeight(h) {
this.height = uni.upx2px(h || 0)
},
initColumn(val) {
val = val + '';
let vals = val.split('');
let digit = vals.length,
arr = [];
for (let i = 0; i < digit; i++) {
if (~numArr.indexOf(vals[i])) {
arr.push(numArr)
} else {
arr.push([vals[i]])
}
}
this.columns = arr;
this.roll(vals)
},
roll(vals) {
let lengths = this.columns.length,
indexs = [];
while (vals.length) {
let num = vals.pop();
if (~numArr.indexOf(num)) {
indexs.unshift(Number(num))
} else {
indexs.unshift(0)
}
}
while (indexs.length < lengths) {
indexs.unshift(0)
}
this.keys = indexs
}
}
}
</script>
<style scoped>
.tui-digital__roller {
display: inline-flex;
justify-content: space-between;
align-items: center;
}
.tui-digital__box {
overflow: hidden;
}
.tui-digital__column {
transform: translate3d(0, 0, 0);
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.tui-digital__item {
display: flex;
align-items: center;
justify-content: center;
}
</style>