Value $ (window) .scrollTop () == $ (document) .height () - $ (window) .height ()

The following code is used to determine if the user scrolls at the bottom of the page and 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, and then compare it with the scroll height to determine if the bottom of the page is reached. Why is it not easy

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

or

if($(window).scrollTop() ==  $(window).height()){
//do something
}
+5
source share
3 answers

This is because it $(window).scrollTop()returns the position of the top of the page, but $(document).height()returns the position of the bottom of the page. Therefore, you need to subtract the height of the window to get a position for comparison, as this will give you the position at which the top of the page will be if you scroll completely to the bottom.

+12

$(window).scrollTop() - . , , 1385, . $(document).height() - (1991 ). $(window).height() - (viewport) (606 ). , - . 1385 + 606 = 1991.

+3

scrollTop . , , .

scrollTop , , .

, , scrollTop , .

0
source

All Articles