js scroll 怎么滚到最下方

2025-03-12 08:48:24
推荐回答(1个)
回答1:

js判断window.scroll 判断滚动到底部的方法是设置一个变量,来检测鼠标位置。
具体的实现方法如下:
$(window).scroll(function() {
如果滚动的高度加上窗口的高度等于页面的高度就是到了底部
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert("bottom!"); //已经滚动到底部
}
});

或者也可以写专用方法检测高度:
function getDocHeight() {
var D = document;
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}

然后再用以下方法检测:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == getDocHeight()) {
alert("bottom!");
}
});