Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 659249 of Window.scrollX

  • Revision slug: Web/API/Window.scrollX
  • Revision title: Window.scrollX
  • Revision id: 659249
  • Created:
  • Creator: AlexChao
  • Is current revision? No
  • Comment

Revision Content

{{ APIRef() }}

Summary

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

Syntax

var x = window.scrollX;

Parameters

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

Example

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

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

See also

Revision Source

<div>
 {{ APIRef() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns the number of pixels that the document has already been scrolled horizontally.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
var x = window.scrollX;</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<ul>
 <li><font face="Courier New, Andale Mono, monospace"><span style="line-height: normal;">x</span></font> is the number of pixels that the document is currently scrolled from the left.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
// If scrollX is greater than 400, reset scroll position&nbsp;to the top left of the document.
if (window.scrollX &gt; 400) {
  window.scroll(0,0);
}</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p><span style="line-height: 1.572;">The </span><code style="font-size: 14px;">pageXOffset</code><span style="line-height: 1.572;"> property is an alias for the </span><code style="font-size: 14px;">scrollX</code><span style="line-height: 1.572;">&nbsp;property:</span></p>
<pre>
window.pageXOffset == window.scrollX; // always true</pre>
<p>For cross-browser compatibility, use <code>window.pageXOffset</code> instead of <code>window.scrollX</code>. <strong>Additionally</strong>, older versions of Internet Explorer (&lt; 9) do not support either property and must be worked around by checking other non-standard properties. A fully compatible example:</p>
<pre class="brush:js">
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;</pre>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
 <li>CSSOM View Module: <a class="external" href="https://dev.w3.org/csswg/cssom-view/#widl-Window-scrollX">window.scrollX</a> (Editor's Draft)</li>
</ul>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/DOM/window.scrollY" title="window.scrollX | Document Object Model (DOM) | MDN">window.scrollY</a></li>
</ul>
Revert to this revision