-1
I’m looking to acquire the percentage that my scroll is, to manipulate css. But I have these two variables:
document.documentElement.scrollTop;
document.documentElement.scrollHeight;
The first one gives me the position of the scroll, and the second one gives me the size of the scroll. Closed, now I just need to split pos/height
to have the percentage of scroll, but in what I realized the variable document.documentElement.scrollTop;
when my scroll is at the end, it does not give the same value as document.documentElement.scrollHeight;
, ie my percentage will never reach 100% if I use this calculation.
From what I understand the document.documentElement.scrollTop;
does not arrive at the end in fact because the position is only caught at the top of the screen. The one way I make that percentage better, like catch the real size of the scroll?
Like
scrollTop
marks the position at the top of the viewport, it will never reach 100%. For your calculation to work just adjust its total value, that is, its total value is the scroll size less the page size (that would be the largest position thatscrollTop
can have). It is a more mathematical problem than programming, the calculation would be:100 * scroll_atual / (scroll_total - tamanho_tela)
– fernandosavio
I think this can help you https://answall.com/questions/312511/howto detect or detect
– hugocsl