iScroll下拉刷新上拉加载(简洁易懂版本)

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
<!DOCTYPE html>
<html>

<head>
<meta charset='UTF-8'>
<title></title>
<style type='text/css'>
* {
margin: 0;
padding: 0;
}

#container {
height: 500px;
overflow: hidden;
position: relative;
width: 600px;
border: 1x solid #ccc;
margin: 10px auto;
}

li {
height: 100px;
background: #fafafa;
border-bottom: 1px solid #ccc;
}
</style>
</head>

<body>
<div id='container'>
<ul>
<li id='pullDown' style='display:none'>下拉刷新</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li id='pullUp' style='display:none'>上拉加载</li>
</ul>
</div>
</body>
<script src='js/iscroll-probe-min.js' type='text/javascript' charset='utf-8'></script>
<script type='text/javascript'>
var _down = document.getElementById('pullDown');
var _up = document.getElementById('pullUp');
var _con = document.getElementById('container');

var iScroll = new IScroll('#container', {
mouseWheel: true,
scrollbars: true,
probeType: 3
})

iScroll.on('scroll', function() {
if(this.y >= 50) {
_down.style.display = 'block';
return;
} else if(this.y < 50 && this.y >= 0) {
_down.style.display = 'none';
return;
}


if(this.maxScrollY - this.y > 50) {
//alert('上拉刷新');
_up.style.display = 'block';
return;
} else if(this.maxScrollY - this.y > 0 && this.maxScrollY - this.y <50 ){
_up.style.display = 'none'
return;
}
})

_con.onmouseup = function(){
if(iScroll.y > 50 || iScroll.maxScrollY - iScroll.y > 50){
setTimeout(function(){
iScroll.refresh();
},300)
}
}
</script>

</html>