document.body元素上的clientHeight, offsetHeight, scrollHeight

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html, body {  height: 100%;  }
        body {
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div style="height:1000px;">

</div>
</body>
<script>
    let clientWidth = document.body.clientWidth;
    let clientHeight = document.body.clientHeight;
    console.log('1. 视口宽:', clientWidth, ' 视口高:', clientHeight);

    let offWidth = document.body.offsetWidth;
    let offHeight = document.body.offsetHeight;
    console.log('2. offsetWidth: ', offWidth, 'offsetHeight: ', offHeight);

    let scrollWidth = document.body.scrollWidth;
    let scrollHeight = document.body.scrollHeight;
    console.log('3. scrollWidth: ', scrollWidth, ' scrollHeight: ', scrollHeight);


</script>
</html>

1、document.body的 clientWidth, clientHeight**(视口大小)**
当给body,html设置height:100%后,body的宽默认100%的情况下,可以使用
document.body.clientWidth ,document.body.clientHeight 取到的值,可以视为视口大小,且chrome,ie,firefox表现一致(javascript高级程序设计第三版8.1.4)。
document.body.clientWidth = body的width+padding*2
当给body设置的宽度不是100%,比如是400px的时候,document.body.clientWidth ,取到的是body的宽。

2、document.body的 offsetWidth, offsetHeight。可以看成,offsetWidth=clientWidth+border = width+pading*2+border*2; offsetHeight同理。

3、document.body的 scrollWidth,scrollHeight。相当于body的 (内容宽高)。
如body中有个div,高度为1000px。那么document.body.scrollHeight = 1000。
如果div有margin,padding,border,那么document.body.scrollHeight = div的高度+margin*2+padding*2+border*2 + body的上padding;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值