I’m hoping to find a way to get the current viewable window’s position (relative to the total page width/height) so I can use it to force a scroll from one section to another. However, there seems to be a tremendous amount of options when it comes to guessing which object holds the true X/Y for your browser.
Which of these do I need to make sure IE 6+, FF 2+, and Chrome/Safari work?
window.innerWidth
window.innerHeight
window.pageXOffset
window.pageYOffset
document.documentElement.clientWidth
document.documentElement.clientHeight
document.documentElement.scrollLeft
document.documentElement.scrollTop
document.body.clientWidth
document.body.clientHeight
document.body.scrollLeft
document.body.scrollTop
And are there any others? Once I know where the window is I can set an event chain that will slowly call window.scrollBy(x,y); until it reaches my desired point.
The method jQuery (v1.10) uses to find this is:
That is:
window.pageXOffsetfirst and uses that if it exists.document.documentElement.scrollLeft.document.documentElement.clientLeftif it exists.The subtraction of
document.documentElement.clientLeft/Toponly appears to be required to correct for situations where you have applied a border (not padding or margin, but actual border) to the root element, and at that, possibly only in certain browsers.Update February 2024:
Nowadays, jQuery basically just uses
window.pageXOffsetandwindow.pageYOffsetwithout any of the rest.Interestingly,
pageXOffsetandpageYOffsetare non-standard. The standards based equivalent isscrollXandscrollY. But all modern browsers place an alias of those topageXOffsetandpageYOffsetfor compatibility, and their use in something as important as jQuery signifies they’re pretty safe to use into the foreseeable future.If you don’t care about Internet Explorer 11 or earlier (which you probably don’t need to at this point), then you can use
scrollXandscrollY.