-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.vue
199 lines (199 loc) · 4.32 KB
/
scroll.vue
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
<template>
<div class="scroll" ref="scroll">
<ul class="scroll-wrapper">
<PullDown ref="dPull" :downText="downText" :listenPullingDown="funcs.listenPullingDown"></PullDown>
<slot></slot>
<PullUp ref="uPull" :upText="upText" :listenPullingUp="funcs.listenPullingUp"></PullUp>
</ul>
</div>
</template>
<script>
import BScroll from 'better-scroll'
import PullDown from './pullDown.vue'
import PullUp from './pullUp.vue'
export default {
props: {
attr: Object,
func: Object,
downText: {
type: Object,
default: () => {
return {
pull: '下拉刷新',
loosen: '松开刷新',
tip: '刷新完成'
}
}
},
upText: {
type: Object,
default: () => {
return {
pull: '正在加载数据',
tip: '已无更多数据'
}
}
}
},
data() {
return {
defaultAttr: {
click: true,
probeType: 1
},
defaultFunc: {
listenBeforeScrollStart: false,
listenScroll: false,
listenScrollEnd: false,
listenScrollToEnd: false,
listenTouchEnd: false,
listenPullingDown: false,
listenPullingUp: false
},
completeFlag: false
}
},
components: {
PullDown,
PullUp
},
computed: {
attrs() {
return Object.assign({}, this.defaultAttr, this.attr)
},
funcs() {
return Object.assign({}, this.defaultFunc, this.func)
}
},
methods: {
calc() {
let res = this.funcs
if (res.listenPullingDown) {
this.$set(this.defaultAttr, 'pullDownRefresh', {
threshold: 58,
stop: 50
})
}
if (res.listenPullingUp) {
this.$set(this.defaultAttr, 'pullUpLoad', {
threshold: 50
})
}
},
init() {
let _this = this
this.BScroll = new BScroll(this.$refs.scroll, this.attrs)
// 滚动开始前
if (this.funcs.listenBeforeScrollStart) {
this.BScroll.on('beforeScrollStart', (pos) => {
this.$emit('beforeScrollStart', pos)
})
}
// 滚动时
if (this.funcs.listenScroll) {
this.BScroll.on('scroll', (pos) => {
this.$emit('scroll', pos)
if (this.funcs.listenPullingDown) {
if (pos.y > this.attrs.pullDownRefresh.threshold && !this.completeFlag) {
this.$refs.dPull.dLoosen()
}
}
if (this.funcs.listenPullingUp) {
if (pos.y <= this.attrs.pullUpLoad.threshold && !this.completeFlag) {
this.$refs.dPull.dPull()
}
}
})
}
// 滚动结束
if (this.funcs.listenScrollEnd) {
this.BScroll.on('scrollEnd', (pos) => {
this.$emit('scrollEnd', pos)
})
}
// 滚动到底部
if (this.funcs.listenScrollToEnd) {
this.BScroll.on('scrollEnd', (pos) => {
if (pos.y <= this.BScroll.maxScrollY + 50) {
this.$emit('scrollToEnd', pos)
}
})
}
// 鼠标、手指离开
if (this.funcs.listenTouchEnd) {
this.BScroll.on('touchEnd', (pos) => {
this.$emit('touchEnd', pos)
})
}
// 监听下拉刷新
if (this.funcs.listenPullingDown) {
this.BScroll.on('pullingDown', () => {
// 触发刷新
_this.$refs.dPull.dHideText()
_this.completeFlag = true
// 派遣函数到父组件去刷新数据
this.$emit('pullingDown')
})
}
// 监听上拉刷新
if (this.funcs.listenPullingUp) {
// 监听上拉加载
this.BScroll.on('pullingUp', () => {
_this.$refs.uPull.uShow()
// 派遣函数到父组件去更新数据
this.$emit('pullingUp')
})
}
},
refresh() {
this.BScroll && this.BScroll.refresh()
},
scrollTo() {
this.BScroll && this.BScroll.scrollTo.apply(this.BScroll, arguments)
},
scrollToElement() {
this.BScroll && this.BScroll.scrollToElement.apply(this.BScroll, arguments)
},
_finishPullDown() {
this.BScroll && this.BScroll.finishPullDown()
},
_finishPullUp() {
this.BScroll && this.BScroll.finishPullUp()
},
afterRefresh() {
let _this = this
this.$refs.dPull.dComplete().then(() => {
setTimeout(() => {
_this.finishPullDown()
setTimeout(() => {
_this.completeFlag = false
_this.refresh()
}, 500)
}, 1000)
})
},
afterUpload(flag = true) {
// 有无结果
if (flag) {
this.$refs.uPull.uHide()
} else {
this.$refs.uPull.uNoResult()
}
this.finishPullUp()
}
},
mounted() {
this.calc()
this.$nextTick(() => {
this.init()
})
}
}
</script>
<style lang='scss' scoped>
.scroll{
width: 100%;
height: 100%;
overflow: hidden;
}
</style>