Vue中下拉刷新和上拉加载更多

下拉刷新

ZDropDownRefresh.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
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
<template lang="html">
<div
class="refresh-moudle"
@touchstart="touchStart($event)"
@touchmove="touchMove($event)"
@touchend="touchEnd($event)"
ref="myrefresh"
:style="{ transform: 'translate3d(0,' + top + 'px, 0)' }"
>
<header class="pull-refresh">
<slot name="pull-refresh">
<div class="down-tip" v-if="dropDownState === 1 || dropDownState === 2">
<i
ref="icon"
class="css-icon icon-upward"
:class="{ active: dropDownState === 2 }"
></i>
<span class="down-text" v-if="dropDownState === 1">{{
dropDownInfo.downText
}}</span>
<span class="down-text" v-if="dropDownState === 2">{{
dropDownInfo.upText
}}</span>
</div>
<div class="refresh-tip" v-if="dropDownState === 3">
<span class="refresh-text">{{ dropDownInfo.refreshText }}</span>
</div>
</slot>
</header>
<slot></slot>
</div>
</template>
<script>
export default {
props: {
onRefresh: {
type: Function,
required: false,
},
},
data() {
return {
defaultOffset: 40, // 默认高度, 相应的修改.releshMoudle的margin-top和.down-tip, .up-tip, .refresh-tip的height
top: 0,
scrollIsToTop: 0,
startY: 0,
isDropDown: false, // 是否下拉
isRefreshing: false, // 是否正在刷新
isDropInTop: false, //开始下拉时是否在滚动条已在最上面
dropDownState: 1, // 显示1:下拉可以刷新, 2:松开立即刷新, 3:正在刷新数据中...
dropDownInfo: {
downText: "下拉可以刷新",
upText: "松开立即刷新",
refreshText: "正在刷新数据...",
refreshImg: "loading.png",
},
};
},
created() {
if (document.querySelector(".down-tip")) {
// 获取不同手机的物理像素(dpr),以便适配rem
this.defaultOffset =
document.querySelector(".down-tip").clientHeight || this.defaultOffset;
}
},
methods: {
/**
* 触摸开始,手指点击屏幕时
* @param {object} e Touch 对象包含的属性
*/
touchStart(e) {
let dom = this.$refs["myrefresh"];
this.scrollIsToTop =
dom.scrollTop || window.pageYOffset || document.body.scrollTop; // safari 获取scrollTop用window.pageYOffset
if (this.scrollIsToTop === 0) {
this.startY = e.targetTouches[0].pageY;
this.isDropInTop = true;
} else {
this.isDropInTop = false;
}
},

/**
* 接触点改变,滑动时
* @param {object} e Touch 对象包含的属性
*/
touchMove(e) {
if (this.isDropInTop) {
if (e.targetTouches[0].pageY > this.startY) {
// 下拉
this.isDropDown = true;
if (!this.isRefreshing) {
// 拉动的距离
let diff =
e.targetTouches[0].pageY - this.startY - this.scrollIsToTop;
this.top =
Math.pow(diff, 0.8) +
(this.dropDownState === 3 ? this.defaultOffset : 0);
if (this.top >= this.defaultOffset) {
this.dropDownState = 2;
e.preventDefault();
} else {
this.dropDownState = 1;
// 去掉会导致ios无法刷新
e.preventDefault();
}
}
} else {
this.isDropDown = false;
this.dropDownState = 1;
}
}
},

/**
* 触摸结束,手指离开屏幕时
*/
touchEnd() {
if (this.isDropInTop) {
if (this.isDropDown && !this.isRefreshing) {
if (this.top >= this.defaultOffset) {
this.refresh();
this.isRefreshing = true;
} else {
// cancel refresh
this.isRefreshing = false;
this.isDropDown = false;
this.dropDownState = 1;
this.top = 0;
}
}
}
},

/**
* 刷新
*/
refresh() {
this.dropDownState = 3;
this.top = this.defaultOffset;
setTimeout(() => {
this.onRefresh(this.refreshDone);
}, 300);
},

/**
* 刷新完成
*/
refreshDone() {
this.isRefreshing = false;
this.isDropDown = false;
this.dropDownState = 1;
this.top = 0;
},
},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.refresh-moudle {
width: 100%;
height: calc(100% + 40px);
margin-top: -40px;
-webkit-overflow-scrolling: touch; /* ios5+ */
overflow-y: scroll;
}

.pull-refresh {
width: 100%;
color: #999;
height: 40px;
transition-duration: 200ms;
font-size: 18px;
}

.refresh-moudle .down-tip,
.up-tip,
.refresh-tip {
display: flex;
align-items: center;
justify-content: center;
height: 40px;
}

.css-icon {
display: inline-block;
height: 1em;
width: 1em;
font-size: 20px;
margin-right: 10px;
box-sizing: border-box;
text-indent: -9999px;
vertical-align: middle;
position: relative;
}

.css-icon::before,
.css-icon::after {
content: "";
box-sizing: inherit;
position: absolute;
left: 50%;
top: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}

.icon-upward::before {
height: 0.65em;
width: 0.65em;
border-style: solid;
border-width: 2px 0 0 2px;
-ms-transform: translate(-50%, -50%) rotate(45deg);
transform: translate(-50%, -50%) rotate(45deg);
}

.icon-upward::after {
height: 0.8em;
border-left: 2px solid;
top: 55%;
}

.icon-upward.active {
transform: rotate(180deg);
transition: transform 0.3s;
}

@keyframes anticlockwise {
0% {
transform: rotate(-180deg);
}
100% {
transform: rotate(0deg);
}
}

@keyframes clockwise {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(-180deg);
}
}

.refresh-img {
width: 35px;
height: 35px;
margin-right: 15px;
animation: rotating 1.5s linear infinite;
}

@keyframes rotating {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(1turn);
}
}
</style>

使用方式

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
<template>
<div class="home">
<div class="msg_list">
<z-refresh :onRefresh="onRefresh">
<div class="msg_item" @click="notice_click">
<div class="msg_top">11111</div>
<div class="msg_info">
123456
</div>
<div class="msg_time">2021-08-28</div>
</div>

<div class="loadmore" @click="onLoadMore">点击加载更多</div>
</z-refresh>
</div>
</div>
</template>

<script>
import ZDropDownRefresh from "../components/ZDropDownRefresh";

export default {
components: {
"z-refresh": ZDropDownRefresh,
},
methods: {
/**
* 下拉刷新
*/
onRefresh(done) {
// 如果下拉刷新和上拉加载同时使用,下拉时初始化上拉的数据
console.info("下拉刷新");
done();
},
onLoadMore() {
console.info("加载更多");
},
},
};
</script>