javascript $(window).scrollTop() == $(document).height() - $(window).height() 的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15690541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:42:29  来源:igfitidea点击:

Meaning of $(window).scrollTop() == $(document).height() - $(window).height()

javascriptjqueryhtml

提问by Usman Mutawakil

The following code is used to detect if a user has scrolled to the bottom of the page and it works.

以下代码用于检测用户是否已滚动到页面底部并且可以正常工作。

if($(window).scrollTop() == $(document).height() - $(window).height()){
//do something
}

Problem:

问题:

I don't understand why you subtract the height of the window from the height of the document, then compare that to the scroll height to determine whether or not the bottom of the page has been reached. Why isn't it simply

我不明白为什么要从文档的高度中减去窗口的高度,然后将其与滚动高度进行比较以确定是否已到达页面底部。为什么不简单

if($(window).scrollTop() == $(document).height()){
//do something
}

or

或者

if($(window).scrollTop() ==  $(window).height()){
//do something
}

回答by CodePB

This is because $(window).scrollTop()returns the position of the top of the page, and $(document).height()returns the position of the bottom of the page. Therefore you need to subtract the height of the window to get the position to compare against, as this will give you the position where the top of the page would be if you were fully scrolled to the bottom.

这是因为$(window).scrollTop()返回页面顶部的位置,并$(document).height()返回页面底部的位置。因此,您需要减去窗口的高度以获得要比较的位置,因为如果您完全滚动到底部,这将为您提供页面顶部的位置。

回答by Explosion Pills

$(window).scrollTop()is the location of the topof the window relative to the document. On the page I'm looking at right now, that's 1385if I'm scrolled to the very bottom. $(document).height()is the height of the entire page (1991for me). $(window).height() is the height of the window (viewport) (606for me). This means that the position of the top of the viewport plus the height of the window is the position of bottomof the viewport. 1385 + 606 = 1991.

$(window).scrollTop()是窗口顶部相对于文档的位置。在我现在正在查看的页面上,1385如果我滚动到最底部。 $(document).height()是整个页面的高度(1991对我来说)。$(window).height() 是窗口(视口)的高度(606对我来说)。这意味着视口顶部的位置加上窗口的高度就是视口底部的位置。 1385 + 606 = 1991.

回答by Guffa

The scrollTopvalue will never be as high as the document height value. That would mean that you scrolled past the document so that it's all outside the window.

scrollTop值永远不会与文档高度值一样高。这意味着您滚动了文档,使其全部位于窗口之外。

Comparing scrollTopto the window height would only mean that you have scrolled one screen down, not to the bottom of the document.

scrollTop窗口高度相比仅意味着您向下滚动一屏,而不是到文档底部。

Subtracting the window height from the document height gives you the value where scrollTopwill be, when the bottom of the window is at the bottom of the document.

scrollTop当窗口的底部位于文档的底部时,从文档高度中减去窗口高度会得到值 where will be。