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 1074988 of Window.scrollX

  • Revision slug: Web/API/Window/scrollX
  • Revision title: Window.scrollX
  • Revision id: 1074988
  • Created:
  • Creator: alexandrudima
  • 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

Specification Status Comment
{{ SpecName('CSSOM View', '#dom-window-scrollx', 'window.scrollX') }} {{ Spec2('CSSOM View') }}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} works in Edge {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}

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>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('CSSOM View', '#dom-window-scrollx', 'window.scrollX') }}</td>
   <td>{{ Spec2('CSSOM View') }}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>works in Edge</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile" style="line-height: 19.0909080505371px;">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<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