offsetWidth、offsetHeight、clientHeight、clientWidth、scrollHeight、scrollWidth详细对比

offsetWidth、offsetHeight、clientHeight、clientWidth、scrollHeight、scrollWidth详细对比

代码测试

  • box-sizing:content-box

请添加图片描述

  • box-sizing:border-box
    请添加图片描述

总结

  • offsetHeight、offsetWidth其实就是元素在屏幕上所占的实际宽高(包括border、padding)
    • 公式:offsetHeight = border + padding + content
    • box-sizing:content-box
      • offsetHeight = 2+10+100 = 112
    • box-sizing:border-box
      • offsetHeight = 2+10+88= 100
  • clientHeight、clientWidth是 padding+conent-滚动条宽度,就拿上述例子来说
    • 公式:clientHeight = padding + conent - 滚动条宽度
    • box-sizing:content-box
      • content高度就是我们设置的宽高,都是100
      • clientHeight = 10+100-17 = 93
    • box-sizing:border-box时,
      • content高度是我们设置的宽高 - padding - border = 100 - 10 - 2 = 88
      • clientHeight = 10+88-17 = 81
  • scrollHeight、scrollWidth是 padding + 实际内容尺寸
    • 出现滚动条时,实际内容尺寸,就是假设没有滚动条,把内容全部铺开的尺寸(注意这里是实际占屏幕的尺寸),这个例子里就是inner元素的尺寸了
    • 公式:scrollHeight = padding + 实际内容尺寸
      • box-sizing:content-box
        • scrollHeight = 10+200 = 210
      • box-sizing:border-box
        • scrollHeight = 10+200= 210
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值