jquery+ajax实现下拉刷新+上拉加载
上面动图是实现的效果。
全程无需重新加载整个页面,只需要上下拉即可刷新数据!
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上拉加载</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
list-style: none;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
html {font-size: 14px; }
header {
height: 40px;
width: 100%;
min-width: 320px;
background: #000;
text-align: center;
color: #fff;
font-size: 1.2rem;
line-height: 40px;
}
footer {
height: 52px;
width: 100%;
min-width: 320px;
background: #000;
text-align: center;
color: #fff;
font-size: 1.2rem;
line-height: 52px;
position: absolute;
bottom: 0;
}
#wrapper {
width: 100%;
min-width: 320px;
position: absolute;
left: 0;
top: 40px;
bottom: 52px;
overflow: hidden;
z-index: 1;
background-color: #eee;
/* 防止本机Windows上的触摸事件 */
-ms-touch-action: none;
/* 防止callout tap-hold和文本的选择 */
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
/* 防止文本调整取向变化对web应用程序很有用 */
-webkit-text-size-adjust: none;
-moz-text-size-adjust: none;
-ms-text-size-adjust: none;
-o-text-size-adjust: none;
text-size-adjust: none;
}
.pull-loading {
text-align: center;
height: 40px;
line-height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
#scroller ul li {
padding: 20px 10px;
border-bottom: solid 1px #ccc;
background: #fff;
}
</style>
</head>
<body>
<header>上拉加载,下拉刷新</header>
<div id="wrapper">
<div id="scroller">
<ul>
<li>这里显示部分数据</li>
<li>这里显示部分数据</li>
<li>这里显示部分数据</li>
<li>这里显示部分数据</li>
<li>这里显示部分数据</li>
</ul>
<div class="pull-loading">
上拉加载
</div>
</div>
</div>
<footer>底部</footer>
<script type="text/javascript" src="js/iscroll.js"></script>
<script type="text/javascript">
var myscroll = new iScroll("wrapper", {
onScrollMove: function () { //拉动时
//上拉加载
if (this.y < this.maxScrollY) {
$(".pull-loading").html("释放加载");
$(".pull-loading").addClass("loading");
} else {
$(".pull-loading").html("上拉加载");
$(".pull-loading").removeClass("loading");
}
},
onScrollEnd: function () { //拉动结束时
//上拉加载
if ($(".pull-loading").hasClass('loading')) {
$(".pull-loading").html("加载中...");
pullOnLoad();
}
}
});
//上拉加载函数,ajax
var num = 0;
var page = 4; //每次加载4条
function pullOnLoad() {
setTimeout(function (