mao-scroll.vue
1.65 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
<template>
<view>
<view class="maoScroll-main" :style="'height:'+(lineHeight*showLine)+'rpx;'">
<view :style="'margin-top:-'+marginTop+'rpx;'">
<view v-for="(item,index) in showdata" :key="'maoScroll'+index" :style="'height:'+lineHeight+'rpx;'">
<slot :line="item" />
</view>
</view>
</view>
</view>
</template>
<script>
export default {
name: 'maoScroll',
data() {
return {
showdata: [],
marginTop: 0,
showLine: 0,
}
},
props:{
data: {
type: Array,
default: []
},
showNum: {
type: Number,
default: 3,
},
lineHeight: {
type: Number,
default: 60,
},
animationScroll: {
type: Number,
default: 500,
},
animation: {
type: Number,
default: 2000,
}
},
methods: {
init: function(){
this.showLine = this.showNum < this.data.length ? this.showNum : this.data.length;
for(let i = 0; i < this.data.length; i++){
this.showdata.push(this.data[i]);
}
for(let i = 0; i < this.showLine; i++){
this.showdata.push(this.data[i]);
}
setInterval(this.animationFunc, this.animation);
},
animationFunc: function(){
if(this.marginTop >= this.data.length*this.lineHeight){
this.marginTop = 0;
}
let stepTime = this.animationScroll/this.lineHeight;
var step = 0;
let self = this;
var index = setInterval(function(){
self.marginTop = self.marginTop + 1;
step++;
if (step >= self.lineHeight) {
clearInterval(index);
}
}, stepTime);
}
},
watch: {
data(outdata, newdata) {
this.init();
}
}
}
</script>
<style>
.maoScroll-main{width: 100%;overflow: hidden;}
</style>