Element.scrollIntoView

  • Revision slug: DOM/element.scrollIntoView
  • Revision title: element.scrollIntoView
  • Revision id: 68554
  • Created:
  • Creator: vali29
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

{{ DomRef() }}

Summary

The scrollIntoView method scrolls the element into view.

Syntax

element.scrollIntoView(alignWithTop);
  • alignWithTop is an optional boolean that if set to true, aligns the scrolled element with the top of the scroll area. If false, it is aligned with the bottom.
  • if no alignWithTop parameter is provided, the element is aligned with the top.

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

HTML5: 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 <strong>scrollIntoView</strong> method scrolls the element into view.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">element.scrollIntoView(alignWithTop);
</pre>
<ul> <li>alignWithTop is an optional boolean that if set to true, aligns the scrolled element with the top of the scroll area. If false, it is aligned with the bottom.</li> <li>if no alignWithTop parameter is provided, the element is aligned with the top.</li>
</ul>
<h3 name="Example">Example</h3>
<pre>&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://www.w3.org/TR/html5/editing.html#scrolling-elements-into-view" title="http://www.w3.org/TR/html5/editing.html#scrolling-elements-into-view">HTML5: Scrolling elements into view</a></p>
<p> </p>
<p>{{ languages( { "fr": "fr/DOM/element.scrollIntoView", "ja": "ja/DOM/element.scrollIntoView" } ) }}</p>
Revert to this revision