vue.vue
4.38 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
<template>
<div class="msg-bar" :style="[barStyle]">
<div
class="msg-container"
:style="{
transform: 'translateY(-' + top + unit + ')',
transition: 'all ' + (speed + 100) + 'ms linear'
}"
>
<div
v-for="(item, idx) in list"
:key="item.id"
class="msg-item"
:style="{ top: item.idx * space + unit }"
>
<slot name="item" :data="item.data">
{{ item.data }}
</slot>
</div>
</div>
</div>
</template>
<script>
let timer = null
export default {
props: {
speed: {
type: Number,
default: 2000 // ms
},
// 间距
space: {
type: Number,
default: 30 // px
},
// 最大渲染熟练
maxLen: {
type: Number,
default: 20
},
// 单位
unit: {
type: String,
default: 'px' // px
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '200rpx'
}
},
data() {
return {
// 是否是暂停状态
isStop: true,
isAutoStop: false,
top: 0,
startIdx: 0,
startId: 0,
list: [],
// 存储所有外部传入的数组 按照顺序
cachedList: []
}
},
computed: {
barStyle() {
return {
width: this.width,
height: this.height
}
}
},
created() {
this.clear()
},
mounted() {
// this.addData(['1', '2', '3', '4', '5', '6'])
this.start()
// #ifdef H5
this.addListener()
// #endif
},
beforeDestroy() {
this.clear()
// #ifdef H5
this.removeListener()
// #endif
},
methods: {
// 监听页面显示 开始 页面隐藏 停止
addListener() {
document.addEventListener('visibilitychange', () => {
this.toggleStart(document.visibilityState === 'visible')
})
},
// 移除监听器
removeListener() {
document.removeEventListener('visibilitychange', () => {
this.toggleStart(document.visibilityState === 'visible')
})
},
// 切换状态
toggleStart(isStart) {
if (isStart) {
this.start()
} else {
this.stop()
}
},
// 添加数据
addData(arr) {
if (!Array.isArray(arr)) {
return console.error('addData 参数必须是一个数组!')
}
// 存储数据
arr.forEach(i => {
this.cachedList.push({
id: this.startId,
data: i
})
this.startId++
})
if (this.list.length === 0) {
/* 初始化 list */
this.initData()
}
},
/* 初始化 list */
initData() {
const len = this.cachedList.length
if (len) {
for (let i = 0; i < this.maxLen; i++) {
// 如果 传入 arr 大于最大长度
if (i < len) {
this.addItem(this.cachedList[i])
} else {
this.addItem(this.cachedList[i % len])
}
}
}
},
// 循环
loopItem() {
if (this.list.length > this.maxLen + 50) {
this.list.splice(0, 50)
}
let idx = this.cachedList.findIndex(i => this.list[this.list.length - 1].id === i.id)
this.addItem(this.cachedList[idx + 1] || this.cachedList[0])
},
// 增加项
addItem(data) {
this.list.push({ ...data, idx: this.startIdx })
this.startIdx++
},
// 开始
start() {
if (!timer && this.list.length > 0) {
this.isStop = false
this.animate()
}
},
// 停止
autoStop() {
if (!this.isStop) {
this.isAutoStop = true
this.clear()
}
},
// 停止
autoStart() {
if (this.isAutoStop) {
this.start()
this.isAutoStop = false
}
},
// 停止
stop() {
this.isStop = true
},
// 动画
animate() {
console.log('animate', timer)
timer = setInterval(() => {
if (this.isStop) {
this.clear()
} else {
this.top += this.space
this.loopItem()
}
}, this.speed)
},
// 清除动画
clear() {
clearInterval(timer)
timer = null
}
}
}
</script>
<style lang="scss" scope>
.msg-bar {
overflow: hidden;
.msg-container {
position: relative;
width: 100%;
// transition: all 1s linear;
.msg-item {
position: absolute;
left: 0;
right: 0;
}
}
}
</style>