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.


var x = window.scrollX;


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.

En termes techniques, scrollX retourne la position X de la fenetre. Si la page n'a pas bougé, la fonction retournera 0.


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) { // Si la page a assez défilé,
  window.scroll(0,0); // remettre la page tout en haut (x à 0) 
et à gauche (y à 0)


La propriété pageXOffset revient à la propriété scrollX :

window.pageXOffset == window.scrollX; // Toujours vrai (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 Status Comment
CSS Object Model (CSSOM) View Module
La définition de 'window.scrollX' dans cette spécification.
Version de travail  

Browser compatibility

Nous convertissons les données de compatibilité dans un format JSON. Ce tableau de compatibilité utilise encore l'ancien format car nous n'avons pas encore converti les données qu'il contient. Vous pouvez nous aider en contribuant !

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Oui) (Oui) (Oui) Pas de support (Oui) (Oui)
Subpixel precision (Oui) (Oui) 55 (55) Pas de support (Oui) (Oui)
Feature Android Android Webview Edge Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? (Oui) (Oui) (Oui) ? ? ? ? (Oui)
Subpixel precision (Oui) (Oui) (Oui) 55.0 (55) ? ? ? ? (Oui)

See also