Window pageXOffset
and pageYOffset
Properties
Definition and Usage
The pageXOffset
and pageYOffset
properties return the number of pixels the document has been scrolled horizontally and vertically from the upper left corner of the window.
pageXOffset
sets or returns the X position of the current page relative to the upper left corner of the window's visible area. pageYOffset
sets or returns the Y position of the current page relative to the upper left corner of the window's visible area.
The pageXOffset
and pageYOffset
properties are equivalent to the scrollX
and scrollY
properties.
These properties are read-only.
Syntax
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Property | |||||
---|---|---|---|---|---|
pageXOffset | Yes | 9.0 | Yes | Yes | Yes |
pageYOffset | Yes | 9.0 | Yes | Yes | Yes |
All major browsers support the pageXOffset
and pageYOffset
properties.
Note: IE 8 and earlier versions do not support these properties, but you can use the document.documentElement.scrollLeft
and document.documentElement.scrollTop
properties instead.
Example
Scroll the content by 100 pixels horizontally and vertically, and then alert the pageXOffset
and pageYOffset
:
window.scrollBy(100, 100);
alert("pageXOffset: " + window.pageXOffset + ", pageYOffset: " + window.pageYOffset);
Example
Compatibility solution (using scrollLeft
and scrollTop
for IE8 and earlier versions):
window.scrollBy(100, 100);
if (window.pageXOffset !== undefined) { // All browsers except IE9 and earlier
alert("Horizontal scroll: " + window.pageXOffset
+ ", Vertical scroll: " + window.pageYOffset);
} else { // IE9 and earlier
alert("Horizontal scroll: " + document.documentElement.scrollLeft
+ ", Vertical scroll: " + document.documentElement.scrollTop);
}