Web/API/Window/scrollX

From Get docs


The read-only scrollX property of the Window interface returns the number of pixels that the document is currently scrolled horizontally. This value is subpixel precise in modern browsers, meaning that it isn't necessarily a whole number. You can get the number of pixels the document is scrolled vertically from the scrollY property.

Syntax

var x = window.scrollX;

Value

In practice, the returned value is a double-precision floating-point value indicating the number of pixels the document is currently scrolled horizontally from the origin, where a positive value means the content is scrolled to the left. If the document is rendered on a subpixel-precise device, then the returned value is also subpixel-precise and may contain a decimal component. If the document isn't scrolled at all left or right, then scrollX is 0.

If you need an integer value, you can use Math.round() to round it off.


In more technical terms, scrollX returns the X coordinate of the left edge of the current viewport. If there is no viewport, the returned value is 0.

Example

This example checks the current horizontal scroll position of the document. If it's greater than 400 pixels, the window is scrolled back to the beginning.

if (window.scrollX > 400) {
  window.scroll(0,0);
}

Notes

The pageXOffset property is an alias for the scrollX property:

window.pageXOffset == window.scrollX; // always true

For cross-browser compatibility, use window.pageXOffset instead of window.scrollX. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties. A fully compatible example:

var x = (window.pageXOffset !== undefined)
  ? window.pageXOffset
  : (document.documentElement || document.body.parentNode || document.body).scrollLeft;

var y = (window.pageYOffset !== undefined)
  ? window.pageYOffset
  : (document.documentElement || document.body.parentNode || document.body).scrollTop;

Specification

Specification Status Comment
CSS Object Model (CSSOM) View ModuleThe definition of 'window.scrollX' in that specification. Working Draft  

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
scrollX

Chrome Full support 1


Full support 1


Full support 1

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Edge Full support 12


Full support 12


Full support 12

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Firefox Full support 1


Full support 1


Full support 1

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

IE Full support 9

Alternate Name'

Full support 9

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Opera Full support 9.6


Full support 9.6


Full support 4

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Safari Full support 1


Full support 1


Full support 1

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

WebView Android Full support 1


Full support 1


Full support 1

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Chrome Android Full support 18


Full support 18


Full support 18

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Firefox Android Full support 4


Full support 4


Full support 4

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Opera Android Full support 10.1


Full support 10.1


Full support 10.1

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Safari iOS Full support 1


Full support 1


Full support 1

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Samsung Internet Android Full support 1.0


Full support 1.0


Full support 1.0

Alternate Name'

Alternate Name' Uses the non-standard name: pageXOffset

Subpixel precision Chrome

Full support Yes

Edge

Full support ≤18

Firefox

Full support 55

IE

No support No

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support 55

Opera Android

?

Safari iOS

?

Samsung Internet Android

Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Uses a non-standard name.'
Uses a non-standard name.


See also