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

后端 未结 3 1241
醉酒成梦
醉酒成梦 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:50

    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.

提交回复
热议问题