I’m trying to detect the position of the browser’s scrollbar with JavaScript to decide where in the page the current view is.
My guess is that I have to detect where the thumb on the track is, and then the height of the thumb as a percentage of the total height of the track. Am I over-complicating it, or does JavaScript offer an easier solution than that? What would some code look like?
You can use
element.scrollTopandelement.scrollLeftto get the vertical and horizontal offset, respectively, that has been scrolled.elementcan bedocument.bodyif you care about the whole page. You can compare it toelement.offsetHeightandelement.offsetWidth(again,elementmay be the body) if you need percentages.