运行 ❯
获取你
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <style> #myDIV { height: 250px; width: 250px; overflow: auto; background: coral; } #content { margin:500px; height: 800px; width: 2000px; background: coral; position: relative; } </style> <body> <h1>The Element Object</h1> <h2>The scrollIntoView() Method</h2> <p>Click the buttons to scroll to the top or to the bottom of the element with id="content".</p> <p> <button onclick="scrollToTop()">Scroll to the top</button> <button onclick="scrollToBottom()">Scroll to the bottom</button> </p> <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> const element = document.getElementById("content"); function scrollToTop() { element.scrollIntoView(true); } function scrollToBottom() { element.scrollIntoView(false); } </script> </body> </html>