forked from OpenNHP/opennhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiscroll.html
244 lines (217 loc) · 6.38 KB
/
iscroll.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<title>豆瓣同城 - 音乐 - 北京 | Just a iScroll Demo</title>
<link rel="stylesheet" href="{{assets}}css/amazeui.css"/>
<style>
html,
body,
.page {
height: 100%;
}
#wrapper {
position: absolute;
top: 49px;
bottom: 0;
overflow: hidden;
margin: 0;
width: 100%;
padding: 0 8px;
background-color: #f8f8f8;
}
.am-list {
margin: 0;
}
.am-list > li {
background: none;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
}
.pull-action {
text-align: center;
height: 45px;
line-height: 45px;
color: #999;
}
.pull-action .am-icon-spin {
display: none;
}
.pull-action.loading .am-icon-spin {
display: block;
}
.pull-action.loading .pull-label {
display: none;
}
</style>
</head>
<body>
<div class="page">
<header data-am-widget="header" class="am-header am-header-default">
<h1 class="am-header-title">
同城 - 音乐
</h1>
</header>
<div id="wrapper" data-am-widget="list_news"
class="am-list-news am-list-news-default">
<div class="am-list-news-bd">
<div class="pull-action loading" id="pull-down">
<span class="am-icon-arrow-down pull-label"
id="pull-down-label"> 下拉刷新</span>
<span class="am-icon-spinner am-icon-spin"></span>
</div>
<ul class="am-list" id="events-list">
<li class="am-list-item-desced">
<div class="am-list-item-text">
正在加载内容...
</div>
</li>
</ul>
<div class="pull-action" id="pull-up">
<span class="am-icon-arrow-down pull-label"
id="pull-up-label"> 上拉加载更多</span>
<span class="am-icon-spinner am-icon-spin"></span>
</div>
</div>
</div>
</div>
<script src="{{assets}}js/jquery.min.js"></script>
<script src="{{assets}}js/handlebars.min.js"></script>
<script type="text/x-handlebars-template" id="tpi-list-item">
{{#each this}}
<li class="am-list-item-desced" data-id="{{id}}">
<a href="{{alt}}" class="am-list-item-hd" target="_blank">{{title}}</a>
<div class="am-list-item-text">{{content}}</div>
</li>
{{/each}}
</script>
<script src="{{assets}}js/amazeui.min.js"></script>
<script>
(function($) {
var EventsList = function(element, options) {
var $main = $('#wrapper');
var $list = $main.find('#events-list');
var $pullDown = $main.find('#pull-down');
var $pullDownLabel = $main.find('#pull-down-label');
var $pullUp = $main.find('#pull-up');
var topOffset = -$pullDown.outerHeight();
this.compiler = Handlebars.compile($('#tpi-list-item').html());
this.prev = this.next = this.start = options.params.start;
this.total = null;
this.getURL = function(params) {
var queries = ['callback=?'];
for (var key in params) {
if (key !== 'start') {
queries.push(key + '=' + params[key]);
}
}
queries.push('start=');
return options.api + '?' + queries.join('&');
};
this.renderList = function(start, type) {
var _this = this;
var $el = $pullDown;
if (type === 'load') {
$el = $pullUp;
}
$.getJSON(this.URL + start).then(function(data) {
console.log(data);
_this.total = data.total;
var html = _this.compiler(data.events);
if (type === 'refresh') {
$list.children('li').first().before(html);
} else if (type === 'load') {
$list.append(html);
} else {
$list.html(html);
}
// refresh iScroll
setTimeout(function() {
_this.iScroll.refresh();
}, 100);
}, function() {
console.log('Error...')
}).always(function() {
_this.resetLoading($el);
if (type !== 'load') {
_this.iScroll.scrollTo(0, topOffset, 800, $.AMUI.iScroll.utils.circular);
}
});
};
this.setLoading = function($el) {
$el.addClass('loading');
};
this.resetLoading = function($el) {
$el.removeClass('loading');
};
this.init = function() {
var myScroll = this.iScroll = new $.AMUI.iScroll('#wrapper', {
click: true
});
// myScroll.scrollTo(0, topOffset);
var _this = this;
var pullFormTop = false;
var pullStart;
this.URL = this.getURL(options.params);
this.renderList(options.params.start);
myScroll.on('scrollStart', function() {
if (this.y >= topOffset) {
pullFormTop = true;
}
pullStart = this.y;
// console.log(this);
});
myScroll.on('scrollEnd', function() {
if (pullFormTop && this.directionY === -1) {
_this.handlePullDown();
}
pullFormTop = false;
// pull up to load more
if (pullStart === this.y && (this.directionY === 1)) {
_this.handlePullUp();
}
});
};
this.handlePullDown = function() {
console.log('handle pull down');
if (this.prev > 0) {
this.setLoading($pullDown);
this.prev -= options.params.count;
this.renderList(this.prev, 'refresh');
} else {
console.log('别刷了,没有了');
}
};
this.handlePullUp = function() {
console.log('handle pull up');
if (this.next < this.total) {
this.setLoading($pullUp);
this.next += options.params.count;
this.renderList(this.next, 'load');
} else {
console.log(this.next);
// this.iScroll.scrollTo(0, topOffset);
}
}
};
$(function() {
var app = new EventsList(null, {
api: 'https://api.douban.com/v2/event/list',
params: {
start: 100,
type: 'music',
count: 10,
loc: 'beijing'
}
});
app.init();
});
document.addEventListener('touchmove', function(e) {
e.preventDefault();
}, false);
})(window.jQuery);
</script>
</body>
</html>
<!--http://pnc.co.il/dev/iscroll-5-pull-to-refresh-and-infinite-demo.html-->