u-pull-refresh.vue
7.55 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<template>
<view
class="u-pull-refresh"
@touchstart="onTouchStart"
@touchmove="onTouchMove"
@touchend="onTouchEnd"
@touchcancel="onTouchEnd"
>
<!-- 下拉刷新区域 -->
<view
class="refresh-area"
:style="{ height: refreshDistance + 'px' }"
:class="{ refreshing: isRefreshing }"
>
<!-- 不同状态的插槽 -->
<slot
v-if="refreshStatus === 'pull'"
name="pull"
:distance="refreshDistance"
:threshold="threshold"
>
<!-- 默认下拉状态 -->
<view class="refresh-content">
<view class="refresh-indicator">
<up-icon name="arrow-downward" size="26px"></up-icon>
</view>
<text class="refresh-text">{{ t("up.pullRefresh.pull") }}</text>
</view>
</slot>
<slot
v-else-if="refreshStatus === 'release'"
name="release"
:distance="refreshDistance"
:threshold="threshold"
>
<!-- 默认释放状态 -->
<view class="refresh-content">
<view class="refresh-indicator">
<up-icon name="arrow-upward" size="26px"></up-icon>
</view>
<text class="refresh-text">{{ t("up.pullRefresh.release") }}</text>
</view>
</slot>
<slot
v-else-if="refreshStatus === 'refreshing'"
name="refreshing"
>
<!-- 默认刷新中状态 -->
<view class="refresh-content">
<view class="refresh-indicator">
<view class="spinner"></view>
</view>
<text class="refresh-text">{{ t("up.pullRefresh.refreshing") }}...</text>
</view>
</slot>
</view>
<!-- 内容区域 -->
<view
class="refresh-content-wrapper"
:style="{ transform: `translateY(${contentTranslateY}px)` }"
>
<scroll-view
v-if="useScrollView"
class="scroll-wrapper"
:scroll-y="true"
:enable-back-to-top="enableBackToTop"
:scroll-top="scrollTop"
:lower-threshold="lowerThreshold"
@scroll="handleScroll"
@scrolltolower="handleScrollToLower"
>
<slot></slot>
<!-- 使用 u-loadmore 组件实现上拉加载更多 -->
<u-loadmore
v-if="showLoadmore"
v-bind="loadmoreProps"
/>
</scroll-view>
<view v-else>
<slot></slot>
<!-- 使用 u-loadmore 组件实现上拉加载更多 -->
<u-loadmore
v-if="showLoadmore"
v-bind="loadmoreProps"
/>
</view>
</view>
</view>
</template>
<script>
import { t } from '../../libs/i18n'
export default {
name: 'u-pull-refresh',
props: {
// 是否正在刷新
refreshing: {
type: Boolean,
default: false
},
// 下拉刷新阈值
threshold: {
type: Number,
default: 80
},
// 阻尼系数
damping: {
type: Number,
default: 0.4
},
// 最大下拉距离
maxDistance: {
type: Number,
default: 120
},
// 是否显示加载更多
showLoadmore: {
type: Boolean,
default: false
},
// u-loadmore 组件的 props 配置
loadmoreProps: {
type: Object,
default: () => ({
status: 'loadmore',
// loadmoreText: '加载更多',
// loadingText: '正在加载...',
// nomoreText: '没有更多了'
})
},
// 是否使用 scroll-view 包装内容
useScrollView: {
type: Boolean,
default: true
},
// scroll-view 相关属性
enableBackToTop: {
type: Boolean,
default: false
},
lowerThreshold: {
type: [Number, String],
default: 50
},
scrollTop: {
type: [Number, String],
default: 0
}
},
data() {
return {
// 下拉刷新相关
isRefreshing: false,
refreshStatus: 'pull', // pull, release, refreshing
refreshDistance: 0,
startY: 0,
currentY: 0,
touching: false,
// 动画相关
contentTranslateY: 0
}
},
emits: ['refresh', 'loadmore', 'scroll'],
watch: {
refreshing: {
handler(newVal) {
if (!newVal) {
this.finishRefresh()
} else {
this.startRefresh()
}
}
}
},
methods: {
t,
// 触摸开始
onTouchStart(e) {
if (this.isRefreshing) return
this.touching = true
this.startY = e.touches[0].pageY
this.currentY = this.startY
this.refreshStatus = 'pull'
},
// 触摸移动
onTouchMove(e) {
if (!this.touching || this.isRefreshing) return
this.currentY = e.touches[0].pageY
const diff = this.currentY - this.startY
// 只有在顶部且下拉时才触发下拉刷新
if (diff > 0 && this.isScrollViewAtTop()) {
this.refreshDistance = Math.min(diff * this.damping, this.maxDistance)
this.contentTranslateY = this.refreshDistance
// 更新状态
if (this.refreshDistance >= this.threshold) {
this.refreshStatus = 'release'
} else {
this.refreshStatus = 'pull'
}
// 阻止默认滚动行为,防止触发页面级滚动
e.preventDefault()
e.stopPropagation()
}
},
// 触摸结束
onTouchEnd() {
if (!this.touching) return
this.touching = false
if (this.refreshDistance >= this.threshold && !this.isRefreshing) {
// 触发刷新
this.startRefresh()
this.$emit('refresh')
} else {
// 回弹
this.resetRefresh()
}
},
// 开始刷新
startRefresh() {
this.isRefreshing = true
this.refreshStatus = 'refreshing'
this.refreshDistance = this.threshold
this.contentTranslateY = this.threshold
},
// 完成刷新
finishRefresh() {
this.isRefreshing = false
this.refreshStatus = 'pull'
this.resetRefresh()
},
// 重置刷新状态
resetRefresh() {
this.refreshDistance = 0
this.contentTranslateY = 0
},
// 检查 scroll-view 是否在顶部
isScrollViewAtTop() {
// 这里可以更精确地判断,但简单起见直接返回 true
// 实际项目中可能需要通过 scroll 事件获取 scrollTop 判断
return true
},
// 处理滚动事件
handleScroll(e) {
this.$emit('scroll', e)
},
// 处理滚动到底部事件
handleScrollToLower(e) {
// 只有当 loadmore 状态为 loadmore 时才触发
if (this.showLoadmore && this.loadmoreProps.status === 'loadmore') {
this.$emit('loadmore')
}
}
}
}
</script>
<style scoped lang="scss">
.u-pull-refresh {
position: relative;
height: 100%;
overflow: hidden;
}
.refresh-area {
position: absolute;
left: 0;
right: 0;
top: 0;
display: flex;
align-items: flex-end;
justify-content: center;
overflow: hidden;
transition: height 0.2s ease-out;
}
.refresh-content-wrapper {
height: 100%;
transition: transform 0.2s ease-out;
}
.scroll-wrapper {
height: 100%;
}
.refresh-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
padding-bottom: 10px;
}
.spinner {
width: 16px;
height: 16px;
border: 2px solid #f3f3f3;
border-top: 2px solid #666;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.refresh-text {
font-size: 14px;
color: #666;
}
</style>