运行 ❯
获取您
自己
的网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <style> #myDIV { height: 250px; width: 250px; overflow: auto; } #content { height: 800px; width: 2000px; background-color: coral; } </style> <body> <h1>The Element Object</h1> <h2>The scrollTop and scrollLeft Properties</h2> <p>Scroll the content below to display the number of pixels it is scrolled.</p> <div id="myDIV" onscroll="myFunction()"> <div id="content">Scroll me!</div> </div> <p id="demo"></p> <script> function myFunction() { const element = document.getElementById("myDIV"); let x = element.scrollLeft; let y = element.scrollTop; document.getElementById ("demo").innerHTML = "Horizontally: " + x.toFixed() + "<br>Vertically: " + y.toFixed(); } </script> </body> </html>