Javascript:如何检测浏览器窗口是否滚动到底部?

And*_*rew 165 javascript

我需要检测用户是否滚动到页面底部.如果它们位于页面底部,当我向底部添加新内容时,我会自动将它们滚动到新的底部.如果他们不在底部,他们正在阅读页面上较高的先前内容,所以我不想自动滚动它们,因为他们想要留在原地.

如何检测用户是否滚动到页面底部或是否在页面上滚动更高?

mVC*_*Chr 231

window.onscroll = function(ev) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        // you're at the bottom of the page
    }
};
Run Code Online (Sandbox Code Playgroud)

见演示

  • 当html/body元素设置为100%时(因为主体填充整个视口高度)不起作用) (24认同)
  • 我使用document.body.scrollHeight而不是offsetHeight(在我的例子中,offsetHeight总是小于window.innerHeight) (8认同)
  • 对于IE,使用`document.documentElement.scrollTop`而不是`window.scrollY`.不确定哪个版本的IE支持`window.scrollY`. (5认同)
  • 在Chromium版本47.0.2526.73(在基本OS 0.3.2(64位)上运行的Ubuntu 14.04上构建)中不起作用 (2认同)

Dek*_*kel 94

所有主要浏览器支持的更新代码(包括IE10和IE11)

window.onscroll = function(ev) {
    if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
        alert("you're at the bottom of the page");
    }
};
Run Code Online (Sandbox Code Playgroud)

当前接受的答案的问题是window.scrollY在IE中不可用.

下面是一个报价MDN关于scrollY:

对于跨浏览器兼容性,请使用window.pageYOffset而不是window.scrollY.

一个工作片段:

window.onscroll = function(ev) {
    if ((window.innerHeight + window.pageYOffset ) >= document.body.offsetHeight) {
        alert("you're at the bottom of the page");
    }
};
Run Code Online (Sandbox Code Playgroud)
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
Run Code Online (Sandbox Code Playgroud)

注意mac

根据@Raphaël的评论,由于偏移很小,因此mac存在问题.
以下更新条件有效:

(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
Run Code Online (Sandbox Code Playgroud)

我没有机会进一步测试,如果有人可以对这个具体问题发表意见,那就太棒了.

  • 听起来很奇怪,只有在我的浏览器中,我才缺少 1 像素,因此不会触发条件。不知道为什么,必须添加额外的几个像素才能使其工作。 (3认同)
  • 在Mac计算机上,由于小偏移(~1px)我们更新了条件,因此不满足下面的条件``(window.innerHeight + window.pageYOffset)> = document.body.offsetHeight - 2` (3认同)
  • thx @Dekel!实际上,我们发现window.pageYOffset是mac上的浮点数.我们的最终解决方案是`(window.innerHeight + Math.ceil(window.pageYOffset + 1))> = document.body.offsetHeight`. (3认同)
  • 在身体具有将高度设置为100%的样式的情况下不起作用 (2认同)

Rya*_*ell 45

接受的答案对我不起作用.这样做:

window.onscroll = function(ev) {
    if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
      // you're at the bottom of the page
      console.log("Bottom of page");
    }
};
Run Code Online (Sandbox Code Playgroud)

如果您希望支持旧浏览器(IE9),请使用window.pageYOffset稍微更好支持的别名.


pas*_*sti 21

我正在寻找答案,但还没有找到答案.这是一个纯粹的JavaScript解决方案,可以在回答这个问题时使用最新的Firefox,IE和Chrome:

// document.body.scrollTop alone should do the job but that actually works only in case of Chrome.
// With IE and Firefox it also works sometimes (seemingly with very simple pages where you have
// only a <pre> or something like that) but I don't know when. This hack seems to work always.
var scrollTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;

// Grodriguez's fix for scrollHeight:
// accounting for cases where html/body are set to height:100%
var scrollHeight = (document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight;

// >= is needed because if the horizontal scrollbar is visible then window.innerHeight includes
// it and in that case the left side of the equation is somewhat greater.
var scrolledToBottom = (scrollTop + window.innerHeight) >= scrollHeight;

// As a bonus: how to scroll to the bottom programmatically by keeping the horizontal scrollpos:
// Since window.innerHeight includes the height of the horizontal scrollbar when it is visible
// the correct vertical scrollTop would be
// scrollHeight-window.innerHeight+sizeof(horizontal_scrollbar)
// Since we don't know the visibility/size of the horizontal scrollbar
// we scroll to scrollHeight that exceeds the value of the
// desired scrollTop but it seems to scroll to the bottom with all browsers
// without problems even when the horizontal scrollbar is visible.
var scrollLeft = (document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft;
window.scrollTo(scrollLeft, scrollHeight);
Run Code Online (Sandbox Code Playgroud)

  • 这几乎对我有用,但我不得不使用`((document.documentElement && document.documentElement.scrollHeight)|| document.body.scrollHeight)`而不仅仅是`document.body.scrollHeight`来说明html /的情况身体设置高度:100% (4认同)

Ife*_*adi 10

这有效

window.onscroll = function() {
    var scrollHeight, totalHeight;
    scrollHeight = document.body.scrollHeight;
    totalHeight = window.scrollY + window.innerHeight;

    if(totalHeight >= scrollHeight)
    {
        console.log("at the bottom");
    }
}
Run Code Online (Sandbox Code Playgroud)


Jer*_*ier 6

如果您height: 100%在某个容器上设置<div id="wrapper">,则以下代码有效(在 Chrome 中测试):

var wrapper = document.getElementById('wrapper');

wrapper.onscroll = function (evt) {
  if (wrapper.scrollTop + window.innerHeight >= wrapper.scrollHeight) {
    console.log('reached bottom!');
  }
}
Run Code Online (Sandbox Code Playgroud)


ita*_*lom 6

window.onscroll = function(ev) {
    if ((window.innerHeight + Math.ceil(window.pageYOffset)) >= document.body.offsetHeight) {
        alert("you're at the bottom of the page");
    }
};
Run Code Online (Sandbox Code Playgroud)

这个答案将修复边缘情况,这是因为pageYOffsetis doublewhileinnerHeightoffsetHeightare long,所以当浏览器给你信息时,你可能是一个像素短。例如:在页面底部我们有

真的 window.innerHeight = 10.2

真的 window.pageYOffset = 5.4

真的 document.body.offsetHeight = 15.6

我们的计算结果变为:10 + 5.4 >= 16 这是错误的

为了解决这个问题,我们可以Math.ceilpageYOffset值进行处理。

希望有帮助。


小智 6

如果您对其他方法都不感兴趣,请尝试此方法。

window.onscroll = function() {
    const difference = document.documentElement.scrollHeight - window.innerHeight;
    const scrollposition = document.documentElement.scrollTop; 
    if (difference - scrollposition <= 2) {
        alert("Bottom of Page!"); 
    }   
}
Run Code Online (Sandbox Code Playgroud)


Cra*_*ole 5

我刚刚开始研究这个,这里的答案对我有帮助,所以谢谢你。我进行了一些扩展,以便代码在回到 IE7 时都是安全的:

希望这对某人有用。

在这里,有一个小提琴;)

    <!DOCTYPE html>
<html>
<head>
    <style>
        div {
            height: 100px;
            border-bottom: 1px solid #ddd;
        }

        div:nth-child(even) {
            background: #CCC
        }

        div:nth-child(odd) {
            background: #FFF
        }

    </style>
</head>

<body>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</body>

<script type="text/javascript">
console.log("Doc Height = " + document.body.offsetHeight);
console.log("win Height = " + document.documentElement.clientHeight);
window.onscroll = function (ev) {
    var docHeight = document.body.offsetHeight;
    docHeight = docHeight == undefined ? window.document.documentElement.scrollHeight : docHeight;

    var winheight = window.innerHeight;
    winheight = winheight == undefined ? document.documentElement.clientHeight : winheight;

    var scrollpoint = window.scrollY;
    scrollpoint = scrollpoint == undefined ? window.document.documentElement.scrollTop : scrollpoint;

    if ((scrollpoint + winheight) >= docHeight) {
        alert("you're at the bottom");
    }
};
</script>
</html>
Run Code Online (Sandbox Code Playgroud)