How to accurately determine the maximum scroll position of a page?

If I use the formula

document_height - window_height = max_scroll_position 

It evaluates to 1 or 0, although I believe that it should sequentially evaluate to 0. For example, on firefox Open the console, scroll down to /qaru.site / ... and do the following on the console:

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

I get 1 as an answer (don't forget to keep the scroll position to the very bottom) However on jquery.com, doing the above gives 0

What am I missing?

+6
source share
1 answer

After some testing, this extra 1 pixel definitely rounds out the error. All three of these variables (document height, window height, and scroll top) are supported either with some greater internal precision or with the scale of the boundless browser window, and then scaled and rounded as necessary according to the requirements of JavaScript properties. In some cases, crosses that become more likely as the scaling factor increases, these values ​​are rounded so that you see inconsistent results that you saw.

If you do not adjust one of these variables many times faster, based on the values ​​of the other variables, this rounding error is unlikely to cause problems for the user. If you need to configure them often, you should probably cache the adjusted numbers and use your cached versions instead of the numbers displayed in the browser for further calculations.

[Note. In the end, I managed to get a -1 rounding error using the formula in OP, with a 250% increase in Firefox.]

+2
source

Source: https://habr.com/ru/post/921683/


All Articles