forked from wzs28150/coolui-scroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (134 loc) · 3.78 KB
/
index.js
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
/*
* @Title: 滚动插件
* @Descripttion: 实现上拉加载下拉刷新
* @version: 2.0.1
* @Author: wzs
* @Date: 2020-05-01 16:06:20
* @LastEditors: wzs
* @LastEditTime: 2020-09-20 15:36:18
*/
Component({
options: {
multipleSlots: true,
addGlobalClass: true,
},
behaviors: [],
// 属性定义(详情参见下文)
properties: {
scrollOption: {
type: Object,
value: {
pagination: {
page: 1,
totalPage: 0,
limit: 0,
length: 0
},
empty: {
img: 'http://coolui.coolwl.cn/assets/mescroll-empty.png'
},
refresh: {
type: 'default'
},
loadmore: {
type: 'default'
}
}
},
background: {
type: String
}
},
data: {
isRefreshLoading: false,
isNoneLoading: false,
isLoading: true,
triggered: false,
threshold: 0,
upTitle: '下拉刷新',
scrollType: 'refresh', // refresh 和 loadMore 两种模式
lazy: null
},
methods: {
onPulling: function () {
this.setData({
isRefreshLoading: true,
});
},
onRefresh() {
if (this._freshing)
return;
this._freshing = true;
setTimeout(() => {
this.setData({
triggered: false
});
this._freshing = false;
}, 1000);
},
onRestore(e) {
this.triggerEvent("refresh", {
page: this.data.scrollOption.pagination.page,
});
setTimeout(() => {
this.setData({
isRefreshLoading: false,
upTitle: '下拉刷新',
threshold: 0
});
}, 1000);
},
onPullingDiy: function (evt) {
setTimeout(() => {
var p = Math.min(evt.detail.dy / 80, 1);
this.triggerEvent("refreshPulling", {
p: p
});
let upTitle = '';
if (p < 0.5) {
upTitle = '下拉刷新';
} else {
upTitle = '释放加载';
}
this.setData({
isRefreshLoading: true,
threshold: p,
upTitle: upTitle
});
}, 300);
},
lower: function (e) {
if (this.data.lazy) {
clearTimeout(this.data.lazy);
}
if (this.data.scrollOption.pagination.page <= this.data.scrollOption.pagination.totalPage) {
let lazy = setTimeout(() => {
console.log('加载开始:显示loadmore');
this.triggerEvent("loadMore");
}, 800);
this.setData({
lazy: lazy,
});
}
},
scroll: function (e) {
if (this.data.scrollOption.pagination.page < this.data.scrollOption.pagination.totalPage) {
this.setData({
isNoneLoading: false,
isLoading: true,
});
} else {
this.setData({
isNoneLoading: true,
isLoading: false,
});
}
},
loadEnd: function () {
console.log('加载结束:隐藏loadmore');
this.setData({
isLoading: false
});
}
}
})