tui-scale-vertical.vue
6.2 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<template>
<view class="tui-scale__box">
<view class="tui-scale__pointer"
:style="{width:pointerWidth+'px',height:pointerWidth+'px',background:getPointerColor,left:pointerLeft+'px'}"
v-if="isPointer">
<view class="tui-scale__triangle"
:style="{left:pointerWidth+'px',borderBottomWidth:pointerWidth/2+'px',borderTopWidth:pointerWidth/2+'px',borderLeftColor:getPointerColor,borderLeftWidth:pointerWidth+'px'}">
</view>
</view>
<scroll-view :throttle="false" scroll-y scroll-with-animation
:style="{height: scrollHeight + 'px', background: backgroundColor, paddingRight: paddingRight + 'rpx' }"
@scroll="handleScroll" :scroll-top="currentTop">
<view class="tui-scale__flex">
<view class="tui-seat__box" :style="{ height: scrollHeight / 2 + 'px' }"></view>
<view style="display: flex;">
<view class="tui-tickmarks__box" :style="{ width: lWidth + 'rpx',borderTopColor:lineColor }">
<view class="tui-scale__flex" v-for="(m, i) in rulerLength" :key="i">
<view class="tui-tick__marks" v-for="(item, index) in tickMarks" :key="index"
:style="[{ width: getWidth(index, width, mWidth, lWidth), height: singleHeight + 'px',borderBottomColor:lineColor }]">
</view>
</view>
</view>
<view class="tui-scale__flex">
<view class="tui-scale__num"
:style="{ height: singleHeight * 10 + 'px', fontSize: size + 'rpx', paddingLeft: left + 'rpx',color:color }">
{{ min }}
</view>
<view class="tui-scale__num"
:style="{ height: singleHeight * 10 + 'px', fontSize: size + 'rpx', paddingLeft: left + 'rpx',color:color }"
v-for="(e, i) in rulerLength" :key="i">
{{ (i + 1) * interval + min }}
</view>
</view>
</view>
<view class="tui-seat__box" :style="{ height: scrollHeight / 2 - singleHeight * 10 + 'px' }"></view>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
name: 'tuiScaleVertical',
emits: ['change'],
props: {
//默认值
value: {
type: Number,
default: 0
},
//刻度尺外层容器宽度/高度 ,默认为屏幕宽度
height: {
type: Number,
default: 0
},
//刻度尺外层容器背景色
backgroundColor: {
type: String,
default: '#FEFEFE'
},
//刻度尺外层容器 padding-right值 rpx
paddingRight: {
type: [Number, String],
default: 30
},
//刻度尺最小值
min: {
type: Number,
default: 0
},
//刻度尺最大值
max: {
type: Number,
default: 10
},
//标尺每段的间隔,取值必须大于 0,并且可被(max - min)整除
interval: {
type: Number,
default: 1
},
//刻度线长度(最短)rpx
width: {
type: Number,
default: 40
},
//刻度线长度(中等)rpx
mWidth: {
type: Number,
default: 60
},
//刻度线长度(最长)rpx
lWidth: {
type: Number,
default: 80
},
//刻度线颜色
lineColor: {
type: String,
default: '#333'
},
//单个格子高度 px
singleHeight: {
type: Number,
default: 8
},
//刻度值字体大小
size: {
type: [Number, String],
default: 32
},
//刻度值字体颜色
color: {
type: String,
default: '#000'
},
//刻度值距离刻度线距离 rpx
left: {
type: Number,
default: 12
},
//是否需要指针
isPointer: {
type: Boolean,
default: true
},
//刻度尺指针宽度 px
pointerWidth: {
type: Number,
default: 12
},
//刻度尺指针颜色
pointerColor: {
type: String,
default: ''
},
//刻度尺指针left值 px
pointerLeft: {
type: Number,
default: -12
}
},
computed:{
getPointerColor(){
return this.pointerColor || (uni && uni.$tui && uni.$tui.color.primary) || '#5677fc'
}
},
data() {
return {
tickMarks: 10,
rulerLength: 1,
scrollHeight: 0,
currentTop: 0
};
},
created() {
this.getHeight();
this.getLength();
},
mounted() {
this.getTop()
},
watch: {
height(val) {
this.getHeight();
},
max(val) {
this.getLength();
},
min(val) {
this.getLength();
},
value(val) {
this.getTop()
}
},
methods: {
getHeight() {
if (!this.height) {
let sys = uni.getSystemInfoSync();
this.scrollHeight = sys.windowWidth;
} else {
this.scrollHeight = this.height;
}
},
getLength() {
//必须满足整除
this.rulerLength = parseInt((this.max - this.min) / this.interval);
},
getTop() {
let value = this.value;
if (value < this.min) value = this.min;
if (value > this.max) value = this.max;
this.currentTop = this.singleHeight * 10 / this.interval * (value - this.min);
},
getWidth(index, width, mWidth, lWidth) {
let w = width;
if (index === 4) w = mWidth;
if (index === 9) w = lWidth;
return `${w}rpx`;
},
handleScroll(e) {
let scrollTop = e.detail.scrollTop;
let value = this.min + (scrollTop / this.singleHeight / 10 * this.interval);
if (value < this.min) value = this.min;
if (value > this.max) value = this.max;
this.$emit('change', {
value: value
})
}
}
};
</script>
<style scoped>
.tui-scale__box {
position: relative;
display: inline-block;
}
.tui-scroll__view {
display: inline-flex;
flex-direction: column;
}
.tui-tickmarks__box {
display: flex;
flex-direction: column;
border-top: 1px solid;
box-sizing: border-box;
position: relative;
}
.tui-scale__flex {
display: inline-flex;
flex-direction: column;
box-sizing: border-box;
}
.tui-tick__marks {
border-bottom: 1px solid;
box-sizing: border-box;
}
.tui-scale__num {
display: flex;
justify-content: center;
flex-direction: column;
text-align: right;
flex-shrink: 0;
transform: translate(0,-50%);
padding-top: 1px;
box-sizing: border-box;
}
.tui-seat__box {
flex-shrink: 0;
}
.tui-scale__pointer {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 0;
z-index: 10;
}
.tui-scale__triangle {
position: absolute;
top: 0;
width: 0;
height: 0;
border-left-style: solid;
border-top-style: solid;
border-bottom-style: solid;
border-top-color: transparent;
border-bottom-color: transparent;
}
</style>