Element.scrollIntoView

  • Revision slug: Web/API/element.scrollIntoView
  • Revision title: element.scrollIntoView
  • Revision id: 406545
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Moved From DOM/element.scrollIntoView to Web/API/element.scrollIntoView

Revision Content

{{ DomRef() }}

Summary

The scrollIntoView() method scrolls the element into view.

Syntax

element.scrollIntoView(alignWithTop);
alignWithTop {{ optional_inline() }}
If true, the scrolled element is aligned with the top of the scroll area. If false, it is aligned with the bottom.
Note: By default, the element is scrolled to align with the top of the scroll area.

Example

<!DOCTYPE html>
<html>
<head>
<title>ScrollIntoView() example</title>

<script type="text/javascript">
function showIt(elID) {
    var el = document.getElementById(elID);
    el.scrollIntoView(true);
}
</script>

</head>
<body>
  <div style="height: 5em; width: 30em; overflow: scroll;
              border: 1px solid blue;">
    <div style="height: 100px"></div>
    <p id="pToShow">The para to show</p>
    <div style="height: 100px"></div>
  </div>
  <input type="button" value="Show para" 
   onclick="showIt('pToShow');">
</body>
</html>

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 {{ CompatGeckoDesktop(1.0) }} 6 10.0 and possibly older versions 4 and possibly older versions
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? {{ CompatGeckoMobile(1.0) }} ? ? ?

Notes

The element may not be scrolled completely to the top or bottom depending on the layout of other elements.

Specification

Revision Source

<div>
  {{ DomRef() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>scrollIntoView()</strong></code> method scrolls the element into view.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
element.scrollIntoView(alignWithTop);</pre>
<dl>
  <dt>
    <code>alignWithTop</code> {{ optional_inline() }}</dt>
  <dd>
    If <code>true</code>, the scrolled element is aligned with the top of the scroll area. If <code>false</code>, it is aligned with the bottom.</dd>
</dl>
<div class="note">
  <strong>Note:</strong> By default, the element is scrolled to align with the top of the scroll area.</div>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush: html">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;ScrollIntoView() example&lt;/title&gt;

&lt;script type="text/javascript"&gt;
function showIt(elID) {
    var el = document.getElementById(elID);
    el.scrollIntoView(true);
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
  &lt;div style="height: 5em; width: 30em; overflow: scroll;
              border: 1px solid blue;"&gt;
    &lt;div style="height: 100px"&gt;&lt;/div&gt;
    &lt;p id="pToShow"&gt;The para to show&lt;/p&gt;
    &lt;div style="height: 100px"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;input type="button" value="Show para" 
   onclick="showIt('pToShow');"&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h2 id="Browser_Compatibility" name="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>1.0</td>
        <td>{{ CompatGeckoDesktop(1.0) }}</td>
        <td>6</td>
        <td>10.0 and possibly older versions</td>
        <td>4 and possibly older versions</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>?</td>
        <td>{{ CompatGeckoMobile(1.0) }}</td>
        <td>?</td>
        <td>?</td>
        <td>?</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Notes">Notes</h2>
<p>The element may not be scrolled completely to the top or bottom depending on the layout of other elements.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://dev.w3.org/csswg/cssom-view/#dom-element-scrollintoview" title="http://dev.w3.org/csswg/cssom-view/#dom-element-scrollintoview">CSSOM: Scrolling elements into view</a></li>
</ul>
Revert to this revision