源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> #myDIV { height: 250px; width: 250px; overflow: auto; } #content { height: 800px; width: 2000px; background-color: coral; } </style> <body> <h1>元素对象</h1> <h2>scrollTop 与 scrollLeft 属性</h2> <p>滚动滚动条,查看数字变化:</p> <div id="myDIV" onscroll="myFunction()"> <div id="content">滚动我!</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 = "水平方向: " + x.toFixed() + "<br>垂直方向: " + y.toFixed(); } </script> </body> </html>
运行结果: