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
<template>
<div ref="wrapper">
<slot></slot>
</div>
</template>
<script type="text/ecmascript-6">
import BScroll from "better-scroll";
export default {
props: {
probeType: {
type: Number,
default: 1
},
click: {
type: Boolean,
default: true
},
listenScroll: {
type: Boolean,
default: false
},
data: {
type: Array,
default: null
},
pullup: {
type: Boolean,
default: false
},
pulldown: {
type: Boolean,
default: false
},
beforeScroll: {
type: Boolean,
default: false
},
refreshDelay: {
type: Number,
default: 100
},
bounce: {
type: Boolean,
default: true
},
scrollX: {
type: Boolean,
default: false
},
scrollY: {
type: Boolean,
default: true
}
},
mounted() {
setTimeout(() => {
this._initScroll();
}, 20);
},
methods: {
_initScroll() {
if (!this.$refs.wrapper) {
return;
}
this.scroll = new BScroll(this.$refs.wrapper, {
probeType: this.probeType,
click: this.click,
bounce: this.bounce,
scrollX: this.scrollX,
scrollY: this.scrollY
});
if (this.listenScroll) {
let me = this;
this.scroll.on("scroll", pos => {
me.$emit("scroll", pos);
});
}
if (this.pullup) {
this.scroll.on("scrollEnd", () => {
if (this.scroll.y <= this.scroll.maxScrollY + 10) {
// 滚动到底部
this.$emit("pullup");
}
});
}
if (this.pulldown) {
this.scroll.on("touchend", pos => {
// 下拉动作
if (pos.y > 50) {
this.$emit("pulldown");
}
});
}
if (this.beforeScroll) {
this.scroll.on("beforeScrollStart", () => {
this.$emit("beforeScroll");
});
}
// if (this.pos) {
// // this.$emit('beforeScroll')
// this.scroll.scrollTo(this.pos.x,this.pos.y)
// }
},
disable() {
this.scroll && this.scroll.disable();
},
enable() {
this.scroll && this.scroll.enable();
},
refresh() {
this.scroll && this.scroll.refresh();
},
scrollTo(x, y, time) {
this.scroll && this.scroll.scrollTo(x, y, time);
},
scrollToElement(el, time) {
this.scroll && this.scroll.scrollToElement(el, time);
},
scrollToElement() {
this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments);
}
},
watch: {
data() {
setTimeout(() => {
this.refresh();
}, this.refreshDelay);
}
}
};
</script>
<style scoped >
</style>