js中的offsetWidth用法

时间:2023-03-09 14:31:49
js中的offsetWidth用法

offsetWidth  //返回盒模型的宽度(包括width+左右padding+左右border)

<style>

#div1 { width:200px; height:200px; border:2px solid red; padding:5px; margin:1px; background:green;}

</style>

<div id="div1"></div>

js中的offsetWidth用法

以上div1的offsetWidth为width+2*padding+2*border=200+2*5+2*2=214px

ps:offsetLeft = left+margin-left

offsetTop = top+margin-top