Window pageXOffsetandpageYOffsetProperty

Window 对象 参考手册Window Object

Definition and Usage

The pageXOffset and pageYOffset properties return the pixels the document has 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 display area. pageYOffset sets or returns the Y position of the current page relative to the upper-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 number that 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 IE versions do not support this property, but you can use the document.documentElement.scrollLeft and document.documentElement.scrollTop properties.


Examples

Examples

Scroll the content vertically and horizontally by 100 pixels, and pop up pageXOffset and pageYOffset:

window.scrollBy(100, 100); alert("pageXOffset: " + window.pageXOffset + ", pageYOffset: " + window.pageYOffset);

Try it Yourself »

Examples

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 scroll:" + window.pageXOffset + ", vertical scroll:" + window.pageYOffset); } else { //IE9 and earlier versions alert("Horizontal scroll:" + document.documentElement.scrollLeft + ", vertical scroll:" + document.documentElement.scrollTop); }

Try it Yourself »


Window 对象参考手册Window Object Other Extensions