<!DOCTYPE html>
<html> <head> <meta charset="UTF-8"> <title>JS盒子模型屬性</title> <style> * { margin: 0; padding: 0; } .outer { box-sizing: border-box; margin: 20px auto; width: 500px; height: 500px; background: lightcoral; border: 10px solid orangered; position: relative; } .box { box-sizing: border-box; margin: 20px auto; padding: 15px; width: 300px; height: 300px; border: 10px solid lightblue; background: lightcyan; font-size: 18px; line-height: 30px; overflow: auto; } </style> </head> <body> <div class="outer" id="outer"> <div id="box" class="box"> 夫君子之行,靜以修身,儉以養德,非淡泊無以明志,非寧靜無以至遠。夫學須靜也,才須學也,非學無以廣才,非志無以成學。淫漫則不能勵精,險躁則不能冶性,年與時馳,意與日去,遂成枯落,多不接世,悲守窮廬,將復何及~~ </div> </div> <script> function offset(curEle) { let par = curEle.offsetParent, l = curEle.offsetLeft, t = curEle.offsetTop; while (par && par.tagName !== "BODY") { if (!/MSIE 8\.0/.test(navigator.userAgent)) { l += par.clientLeft; t += par.clientTop; } l += par.offsetLeft; t += par.offsetTop; par = par.offsetParent; } return { top: t, left: l }; } </script> </body> </html>複製代碼