Javascript examples for DOM:Element scrollIntoView
The scrollIntoView() method scrolls the element into the visible area.
Parameter | Type | Description |
---|---|---|
alignTo | Boolean | Optional. |
alignTo value:
If omitted, it will scroll to the top of the element.
No return value
Scroll the element with id="content" into the visible area of the browser window:
<!DOCTYPE html> <html> <head> <style> #myDIV {// w w w . j a v a 2 s . com height: 250px; width: 250px; overflow: auto; background: green; } #content { margin:500px; height: 800px; width: 2000px; background-color: coral; position: relative; } </style> </head> <body> <button onclick="scrollToTop()">Scroll to the top of the element</button> <button onclick="scrollToBottom()">Scroll to the bottom of the element</button> <div id="myDIV"> <div id="content"> <div style="position:absolute;top:0;">Some text at the top</div> <div style="position:absolute;bottom:0">Some text at the bottom</div> </div> </div> <script> var elmnt = document.getElementById("content"); function scrollToTop() { elmnt.scrollIntoView(true); // Top } function scrollToBottom() { elmnt.scrollIntoView(false); // Bottom } </script> </body> </html>