clientTop、clientLeft、clientWidth、clientHeight

1.clientTop、clientLeft获取的是上边框、左边框的宽度

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>location</title>
    <style>
        .content {
            position: relative;
            height: 600px;
            width: 600px;
            margin: 0 auto;
            background-color: pink;
            overflow: hidden;
        }
        
        .father {
            margin: 50px auto;
            height: 450px;
            width: 450px;
            background-color: #ccc;
            overflow: hidden;
        }
        
        .son {
            border-top: 10px solid red;
            border-right: 20px solid red;
            border-bottom: 30px solid red;
            border-left: 40px solid red;
            padding: 10px;
            margin: 50px;
            height: 300px;
            width: 300px;
            background-color: #000;
        }
    </style>

    <script>
        window.onload = function() {
            let sonDiv = document.querySelector('.son');
            console.log('clientTop: ' + sonDiv.clientTop);
            console.log('clientLeft: ' + sonDiv.clientLeft);
        }
    </script>
</head>

<body>
    <div class="content">
        <div class="father">
            <div class="son"></div>
        </div>
    </div>
</body>

</html>

这里son盒子的上边框是10px,左边框是40px:

2.clientWidth、clientHeight会计算标签的内边距+内容的宽度或者高度,同样是上面的样式,打印一下son盒子的这两个属性:

window.onload = function() {
    let sonDiv = document.querySelector('.son');
    console.log('clientWidth: ' + sonDiv.clientWidth);
    console.log('clientHeight: ' + sonDiv.clientHeight);
}

这里son盒子的内边距为10px,内容为300px,结果为300+10*2=320px:

这两个属性与offsetWidth、offsetHeight的区别是offset属性包含了边框的宽度.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值