Element.scrollIntoView

  • Revision slug: DOM/element.scrollIntoView
  • Revision title: element.scrollIntoView
  • Revision id: 68557
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment clean up styling and formatting; 19 words added, 18 words removed

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

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

Notes

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

Specification

CSSOM: Scrolling elements into view

{{ languages( { "fr": "fr/DOM/element.scrollIntoView", "ja": "ja/DOM/element.scrollIntoView" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>The <code><strong>scrollIntoView()</strong></code> method scrolls the element into view.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">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>
<h3 name="Example">Example</h3>
<pre class="brush: html">&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>
<h3 name="Notes">Notes</h3>
<p>The element may not be scrolled completely to the top or bottom depending on the layout of other elements.</p>
<h3 name="Specification">Specification</h3>
<p><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></p>
<p>{{ languages( { "fr": "fr/DOM/element.scrollIntoView", "ja": "ja/DOM/element.scrollIntoView" } ) }}</p>
Revert to this revision