<!DOCTYPE html>html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.7.1.js"></script>
<script>
$(document).bind("scroll", srcollEvent);
$(document).bind("touchstart", startEvent).bind("touchmove", moveEvent).bind("touchcancel", stopEvent).bind("touchend", stopEvent);
var isScrollStop = true; //頁面是否中止滾動 防止屏幕有滑動但還沒到底部就開始加載數據
var isMoveDown = false; //防止手指向上滑動屏幕開始加載數據
var isLoading = false; //防止異步請求數據未返回到前端的時候重複提交請求
var isMoved = false; //手指是否在滑動屏幕 防止出現手指觸摸屏幕而沒有滑動就加載數據
var startY = 0;
var startX = 0;
function srcollEvent() {
if ($(document).scrollTop() > 0) {
isScrollStop = false;
}
//若是是左右滑動異步加載數據就用下面的代碼
/*
if ($(document).scrollLeft() > 0) {
isScrollStop = false;
}*/
}
function startEvent() {
startY = event.targetTouches[0].clientY;
//若是是左右滑動加載數據就用下面的代碼
//startX = event.targetTouches[0].clientX;
isScrollStop = true;
isMoved = false;
isMoveDown = false;
}
function moveEvent() {
var Y = event.targetTouches[0].clientY;
if (startY > Y) {
isMoveDown = true;
} else {
isMoveDown = false;
}
//若是是左右滑動加載數據就用下面的代碼
/*var X = event.targetTouches[0].clientX;
if (startX > X) {
isMoveDown = true;
} else {
isMoveDown = false;
}*/
isMoved = true;
}
function stopEvent() {
if (isScrollStop && isMoved && !isLoading && isMoveDown) {
console.log("分頁加載");
loadData();
}
}
var index = 0;
function loadData() { //模擬向後臺異步加載數據
isLoading = true; //異步加載數據以前先設置爲正在等待數據
$("table").append("<tr><td>異步加載數據" + index + "</td></tr>");
$("table").append("<tr><td>異步加載數據" + (index+1) + "</td></tr>");
$("table").append("<tr><td>異步加載數據" + (index+2) + "</td></tr>");
$("table").append("<tr><td>異步加載數據" + (index+3) + "</td></tr>");
$("table").append("<tr><td>異步加載數據" + (index+4) + "</td></tr>");
$("table").append("<tr><td>異步加載數據" + (index+5) + "</td></tr>");
//此時數據已返回到前端
index+=5;
isLoading = false;
isMoved = false;
}
function sleep() {
var t1 = new Date();
var t2 = new Date();
while ((t2 - t1) < 3000) {
t2 = new Date();
}
}
</script>
</head>
<body >
<table style="font-size:100px">
<tr>
<td>測試數據1</td>
</tr>
<tr>
<td>測試數據2</td>
</tr>
<tr>
<td>測試數據3</td>
</tr>
<tr>
<td>測試數據4</td>
</tr>
<tr>
<td>測試數據5</td>
</tr>
<tr>
<td>測試數據6</td>
</tr>
<tr>
<td>測試數據7</td>
</tr>
<tr>
<td>測試數據1</td>
</tr>
<tr>
<td>測試數據2</td>
</tr>
<tr>
<td>測試數據3</td>
</tr>
<tr>
<td>測試數據4</td>
</tr>
<tr>
<td>測試數據5</td>
</tr>
<tr>
<td>測試數據6</td>
</tr>
<tr>
<td>測試數據7</td>
</tr>
</table>
</body>
</html>前端