Window pageXOffset and pageYOffset properties
Window Object
Definition and Usage
The pageXOffset and pageYOffset properties return the number of pixels the document is scrolled horizontally and vertically from the top-left corner of the window.
pageXOffset sets or returns the X position of the current page relative to the top-left corner of the window's display area. pageYOffset sets or returns the Y position of the current page relative to the top-left corner of the window's display area.
The pageXOffset and pageYOffset properties are equivalent to the scrollX and scrollY properties.
These properties are read-only.
Syntax
window.pageXOffset
window.pageYOffset
Browser support
The numbers in the table indicate the first browser version that supports this 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 IE versions do not support these properties, but you can use the document.documentElement.scrollLeft and document.documentElement.scrollTop properties.
Example
Example
Scroll the content 100 pixels vertically and horizontally, and pop up pageXOffset and pageYOffset:
window
.
scrollBy
(
100
,
100
)
;
alert
(
"
pageXOffset:
"
window
.
pageXOffset
"
, pageYOffset:
"
window
.
pageYOffset
)
;
Try it »
Example
Compatibility solution (use scrollLeft and scrollTop in IE8 and earlier versions):
window
.
scrollBy
(
100
,
100
)
;
if
(
window
.
pageXOffset
!==
undefined
)
{
//
All browsers, except IE9 and earlier versions
alert
(
"
Horizontal scrolling:
"
window
.
pageXOffset
"
, vertical scrolling:
"
window
.
pageYOffset
)
;
}
else
{
//
IE9 and earlier versions
alert
(
"
Horizontal scrolling:
"
document
.
documentElement
.
scrollLeft
"
, vertical scrolling:
"
document
.
documentElement
.
scrollTop
)
;
}
Try it »
Window Object