<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body,html{ margin:0px; padding:0px; } #div{ height: 300px;width:200px;background:indianred; padding: 20px; border:20px solid #777 ; margin: 100px; } </style> </head> <body style=""> <div style="height: 40px;background: antiquewhite"></div> <div id="div2" onscroll="myFunction()" style=" 300px;overflow: scroll;position: absolute;height: 200px;background: rebeccapurple"> <div id="div"></div> </div> </body> </html> <script src="./event.js"></script> <script> var dom = document.getElementById('div'); var dom2 = document.getElementById('div2'); console.log(window.getComputedStyle(dom).width) //200px; content console.log(dom.offsetWidth); //280 content + padding + bordder console.log(dom.clientWidth) ;//240 content + padding console.log(dom.scrollWidth) ;//240 content + padding +(溢出尺寸) //当前元素 上边框 外边缘 到 最近的已定位父级(offsetParent) 上边框 内边缘的 距离。如果父 //级都没有定位,则分别是到body 顶部 和左边的距离 console.log(dom.offsetTop); //100 content + padding + bordder console.log(dom.clientTop) ;//20 border-top function myFunction(){ //元素滚动条内的顶部隐藏部分的高度 console.log(dom2.scrollTop); //0 } </script>