vue-roller
Vue.js的流畅滚动动画
基础配置
安装依赖
pnpm add vue-roller@2.1.01
使用示例
vue
<script setup lang="ts">
import { ref } from 'vue'
import { Roller } from 'vue-roller'
import 'vue-roller/dist/style.css'
const value = ref(1234)
const changeValue = () => {
value.value = Math.floor(Math.random() * 9999)
}
</script>
<template>
<div class="box">
<h2>vue-roller 最简示例</h2>
<Roller
:value="String(value)"
:duration="1000"
/>
<button @click="changeValue">
随机更新
</button>
</div>
</template>
<style scoped>
.box {
padding: 40px;
text-align: center;
font-size: 18px;
}
button {
margin-top: 20px;
padding: 8px 14px;
cursor: pointer;
}
</style>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
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

综合示例
vue
<script setup lang="ts">
import { ref, computed } from 'vue'
import { Roller } from 'vue-roller'
import 'vue-roller/dist/style.css'
/** ===== 数据 ===== */
const num = ref(1234)
const text = ref('ABCD')
const emoji = ref('😀😃😄')
/** ===== string 转换(必须) ===== */
const numStr = computed(() => String(num.value))
const textStr = computed(() => text.value)
const emojiStr = computed(() => emoji.value)
/** ===== 更新 ===== */
const changeNum = () => {
num.value = Math.floor(Math.random() * 9999)
}
const changeText = () => {
const chars = 'ABCDEFGHJKMNPQRSTUVWXYZ'
text.value = Array.from({ length: 4 })
.map(() => chars[Math.floor(Math.random() * chars.length)])
.join('')
}
const changeEmoji = () => {
const pool = ['😀', '😃', '😄', '😁', '😆', '😎']
emoji.value = Array.from({ length: 3 })
.map(() => pool[Math.floor(Math.random() * pool.length)])
.join('')
}
</script>
<template>
<div class="page">
<h1>🎰 vue-roller </h1>
<div class="grid">
<!-- 🔢 数字 -->
<div class="card">
<h3>🔢 number</h3>
<Roller
:value="numStr"
char-set="number"
:duration="1200"
/>
<button @click="changeNum">更新数字</button>
</div>
<!-- 🔤 字母 -->
<div class="card">
<h3>🔤 alphabet</h3>
<Roller
:value="textStr"
char-set="alphabet"
:duration="1200"
/>
<button @click="changeText">更新字母</button>
</div>
<!-- 😀 emoji -->
<div class="card">
<h3>😀 自定义字符集</h3>
<Roller
:value="emojiStr"
:char-set="['😀','😃','😄','😁','😆','😎']"
:duration="1400"
/>
<button @click="changeEmoji">更新表情</button>
</div>
</div>
</div>
</template>
<style scoped>
.page {
padding: 40px;
min-height: 100vh;
background: #0b1220;
color: #e5e7eb;
font-family: system-ui;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
.card {
background: #111827;
border: 1px solid #1f2937;
border-radius: 14px;
padding: 20px;
text-align: center;
}
.card h3 {
margin-bottom: 12px;
color: #93c5fd;
}
/* 放大 roller */
.card :deep(.roller) {
font-size: 34px;
font-weight: bold;
color: #38bdf8;
}
button {
margin-top: 12px;
padding: 8px 12px;
border-radius: 8px;
border: none;
cursor: pointer;
background: #2563eb;
color: white;
}
button:hover {
background: #1d4ed8;
}
</style>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
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
