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

后端 未结 3 1249
醉酒成梦
醉酒成梦 2021-02-03 15:10

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).         


        
3条回答
  •  故里飘歌
    2021-02-03 15:53

    The scrollTop value 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.

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

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

提交回复
热议问题