var ScrollLoadInit = { //当前所在页 PageIndex: 1, //是否还在传输中 Is_Submit: false, //返回的值以为空/已经读完了 IsFinally: false, //默认总页数 PageCount: 99999, //默认数据类型 DataType:"json" } function ScrollLoad(type, data, url, callback) { $(window).scroll(function () { var nScrollTop = $(this).scrollTop(); if (nScrollTop >= $(document).height() - $(window).height() - 50) { var result = ToGetData(type, data, url); callback(result); } }) } //post请求 function ScrollLoadPost(data, url, callback) { ScrollLoad("post", data, url, callback); } //get请求 function ScrollLoadGet(data, url, callback) { ScrollLoad("get", data, url, callback); } function ToGetData(type, data, url) { //定义请求成功后返回的对象 var ResultData = ""; //返回一些不能继续加载的情况 if (ScrollLoadInit.Is_Submit) { return ""; } if (ScrollLoadInit.PageIndex > ScrollLoadInit.PageCount) { return ""; } //表示传输中,阻止请求 ScrollLoadInit.Is_Submit = true; //禁止缓存机制 $.ajaxSetup({ cache: false }); $.ajaxSetup({ cache: false }); data.pageIndex = ScrollLoadInit.PageIndex; data.timestamp = new Date().getTime(); //url.indexOf('?') != -1 ? url += "&" : url += "?"; $.ajax({ type: type, data: data, url: url, dataType:ScrollLoadInit.DataType, async: false, success: function (result) { ScrollLoadInit.Is_Submit = false; if (result == "") { ScrollLoadInit.IsFinally = true; ScrollLoadInit.PageCount = ScrollLoadInit.PageIndex - 1; } ResultData = result; } }) ScrollLoadInit.PageIndex += 1; return ResultData; }
相关文章: