<script type="text/javascript">
(function(window) {
var i = 2;
var moreData=true;
// 获取当前滚动条的位置
function getScrollTop() {
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
}
// 获取当前可视范围的高度
function getClientHeight() {
var clientHeight = 0;
if (document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
}
else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
}
// 获取文档完整的高度
function getScrollHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}
var _text = document.querySelector('.refreshText'),
_container = document.getElementById('refreshContainer');
var throttle = function(method, context){
clearTimeout(method.tId);
method.tId = setTimeout(function(){
method.call(context);
}, 300);
}
function fetchData() {
setTimeout(function() {
_container.insertAdjacentHTML('beforeend', '<li>new add...</li>');
}, 1000);
}
window.onscroll = function() {
if(moreData){
if (getScrollTop() + getClientHeight() >= getScrollHeight()) {
_text.innerText = '加载中...';
// throttle(fetchData);
$.ajax({
type: 'POST',
// dataType: 'JSON',
url: '${path}/', //服务路径
data: {
page:i
},
success: function (res) {
console.log("res---" + JSON.stringify(res))
if (res == "") {
console.log("data is null");
_text.innerText = '';
moreData = false;
} else {
_container.insertAdjacentHTML('beforeend', res);
i++;
}
},
error: function () {
$(this).removeAttr("disabled");
alert("换卡异常失败!");
}
});
}
}
};
})(window);
</script>
有时候 会出现放10条数据下拉无响应的情况,因为10条数据没有把页面铺满.触底事件无法执行.可让一页显示20条数据