您也可以使用以下方法,即使我建议使用 xdazz 建议的库版本
//returns the size of the whole html
function getDocHeight() {
var D = document;
return Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}
//returns the size of the browser window
function viewport() {
var e = window, a = 'inner';
if ( !( 'innerWidth' in window ) ){
a = 'client';
e = document.documentElement || document.body;
}
return { width : e[ a+'Width' ] , height : e[ a+'Height' ] }
}
//register a scroll listener, that checks if you are at the end of the page
$(window).scroll(function(){
if( $(window).scrollTop()+viewport()["height"] >= getDocHeight() ){
//your method here
loadMore();
}
});