Window.scrollX

Summary

Returns the number of pixels that the document has already been scrolled horizontally.

Syntax

JavaScript
var x = window.scrollX;

Parameters

  • x is the number of pixels that the document is currently scrolled from the left.

Example

JavaScript
// If scrollX is greater than 400, reset scroll position to the top left of the document.
if (window.scrollX > 400) {
  window.scroll(0,0);
}

Notes

The pageXOffset property is an alias for the scrollX property:

JavaScript
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:

JavaScript
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 Module
The definition of 'window.scrollX' in that specification.
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Microsoft Edge Opera Safari (WebKit)
Basic support (Yes) (Yes) No support (Yes) (Yes) (Yes)
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? (Yes) ? ? ? ? ? (Yes)

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/window/scrollx

API CSSOM View NeedsBrowserCompatibility NeedsMarkupWork NeedsMobileBrowserCompatibility Property Reference Référence