NumberRoll.vue
6.42 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<template>
<view class="number-roll-container">
<view
v-for="(digit, index) in digits"
:key="index"
class="digit-container"
>
<view class="digit-wrapper">
<view
class="digit-column"
:style="{
transform: `translateY(-${digit.currentValue * 10}%) translateZ(0)`,
willChange: isAnimating ? 'transform' : 'auto'
}"
>
<view
v-for="num in 10"
:key="num - 1"
class="digit-item"
>
{{ num - 1 }}
</view>
</view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, computed, watch, defineExpose } from 'vue'
// Props
const props = defineProps({
// 目标数值
targetValue: {
type: Number,
default: 0
},
// 数字位数
digitCount: {
type: Number,
default: 4
},
// 滚动速度(毫秒)
duration: {
type: Number,
default: 2000
},
// 每位数字之间的延迟(毫秒)
digitDelay: {
type: Number,
default: 200
}
})
// 响应式数据
const isAnimating = ref(false)
const isPaused = ref(false)
const animationTimers = ref([])
// 初始化数字数组
const digits = ref([])
// 初始化数字位
const initDigits = () => {
digits.value = Array.from({ length: props.digitCount }, (_, index) => ({
currentValue: 0,
targetValue: 0,
animationId: null,
startTime: 0,
pausedTime: 0
}))
}
// 将数字转换为数组
const parseTargetValue = (value) => {
const str = value.toString().padStart(props.digitCount, '0')
return str.split('').map(Number)
}
// 开始滚动动画
const startAnimation = () => {
if (isAnimating.value && !isPaused.value) return
isAnimating.value = true
isPaused.value = false
const targetDigits = parseTargetValue(props.targetValue)
// 为每个数字位设置目标值
digits.value.forEach((digit, index) => {
digit.targetValue = targetDigits[index]
})
// 依次启动每个数字位的动画
digits.value.forEach((digit, index) => {
const delay = index * props.digitDelay
setTimeout(() => {
if (!isAnimating.value || isPaused.value) return
animateDigit(digit, index)
}, delay)
})
}
// 单个数字位的动画
const animateDigit = (digit, index) => {
const startValue = digit.currentValue
const endValue = digit.targetValue
const startTime = Date.now()
digit.startTime = startTime
const animate = () => {
if (!isAnimating.value || isPaused.value) return
const currentTime = Date.now()
const elapsed = currentTime - startTime
const progress = Math.min(elapsed / props.duration, 1)
// 使用更平滑的缓动函数
const easeProgress = easeOutQuart(progress)
// 计算当前值,使用更精确的计算
const currentValue = startValue + (endValue - startValue) * easeProgress
// 减少不必要的更新,只在值有明显变化时更新
const roundedValue = Math.round(currentValue * 100) / 100
if (Math.abs(digit.currentValue - roundedValue) > 0.01) {
digit.currentValue = roundedValue
}
if (progress < 1) {
digit.animationId = requestAnimationFrame(animate)
} else {
digit.currentValue = endValue
checkAnimationComplete()
}
}
digit.animationId = requestAnimationFrame(animate)
}
// 更平滑的缓动函数
const easeOutQuart = (t) => {
return 1 - Math.pow(1 - t, 4)
}
// 检查动画是否完成
const checkAnimationComplete = () => {
const allComplete = digits.value.every(digit =>
Math.abs(digit.currentValue - digit.targetValue) < 0.01
)
if (allComplete) {
isAnimating.value = false
// 确保所有数字都是整数
digits.value.forEach(digit => {
digit.currentValue = digit.targetValue
})
}
}
// 暂停动画
const pauseAnimation = () => {
if (!isAnimating.value || isPaused.value) return
isPaused.value = true
digits.value.forEach(digit => {
if (digit.animationId) {
cancelAnimationFrame(digit.animationId)
digit.animationId = null
digit.pausedTime = Date.now()
}
})
}
// 恢复动画
const resumeAnimation = () => {
if (!isAnimating.value || !isPaused.value) return
isPaused.value = false
digits.value.forEach((digit, index) => {
if (digit.currentValue !== digit.targetValue) {
// 调整开始时间以补偿暂停的时间
const pausedDuration = digit.pausedTime - digit.startTime
digit.startTime = Date.now() - pausedDuration
animateDigit(digit, index)
}
})
}
// 重置动画
const resetAnimation = () => {
isAnimating.value = false
isPaused.value = false
// 取消所有动画
digits.value.forEach(digit => {
if (digit.animationId) {
cancelAnimationFrame(digit.animationId)
digit.animationId = null
}
digit.currentValue = 0
digit.targetValue = 0
digit.startTime = 0
digit.pausedTime = 0
})
}
// 监听目标值变化
watch(() => props.targetValue, () => {
if (isAnimating.value) {
resetAnimation()
}
})
// 初始化
initDigits()
// 暴露方法
defineExpose({
start: startAnimation,
pause: pauseAnimation,
resume: resumeAnimation,
reset: resetAnimation,
isAnimating: computed(() => isAnimating.value),
isPaused: computed(() => isPaused.value)
})
</script>
<style lang="less">
.number-roll-container {
display: flex;
align-items: center;
justify-content: center;
gap: 4rpx;
}
.digit-container {
position: relative;
width: 40rpx;
height: 60rpx;
overflow: hidden;
background: rgba(255, 255, 255, 0.1);
border-radius: 8rpx;
border: 1rpx solid rgba(255, 255, 255, 0.2);
// 启用硬件加速
transform: translateZ(0);
backface-visibility: hidden;
perspective: 1000px;
}
.digit-wrapper {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
// 启用硬件加速
transform: translateZ(0);
}
.digit-column {
position: absolute;
top: 0;
left: 0;
width: 100%;
// 移除CSS transition,完全由JS控制动画
// transition: transform 0.1s ease-out;
// 启用硬件加速和优化
transform-style: preserve-3d;
backface-visibility: hidden;
}
.digit-item {
width: 100%;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 32rpx;
font-weight: bold;
color: #fff;
font-family: 'Courier New', monospace;
// 文本渲染优化
text-rendering: optimizeSpeed;
-webkit-font-smoothing: antialiased;
// 启用硬件加速
transform: translateZ(0);
}
</style>