offset

本文详细解释了HTML和CSS中的DOM元素定位属性,如offsetParent用于获取带有定位的父元素,offsetWidth和offsetHeight提供元素尺寸(不包括边距),以及offsetTop和offsetLeft表示元素相对于其父元素的偏移量。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/base/base.css">
</head>
<style>
    .box {
        position: relative;
        width: 100%;
        height: 100%;
        background-color: pink;
    }

    .boxChildren {
        width: 200px;
        height: 200px;
        padding: 20px;
        margin-left: 20px;
        border: 20px solid black;
        background-color: red;
    }
</style>

<body>
    <div class="box">
        <div class="boxChildren">
        </div>

    </div>
    <script>
        const box = document.querySelector('.box');
        const boxChildren = box.querySelector(".boxChildren");
        // offet家族全是只读属性,无单位数值
        //offsetParent带有定位的父元素如果没有继续向上找到直到body 如果元素是fixed父元素是null
        console.log(boxChildren.offsetParent, "获取父元素");
        //offsetWidth获取元素的宽度返回不带单位的数值,ElementWidth+boxred(left+right)+padding(left+right)
        console.log(boxChildren.offsetWidth, "元素宽度");
        //offsetHeight获取元素的高度返回不带单位的数值,ElementHeight+boxred(top+bottom)+padding(top+bottom)
        console.log(boxChildren.offsetHeight, "元素高度");
        //offsetTop获取元素的上偏移量从上边到自身的border结束,获取的是数值,相对父元素
        console.log(boxChildren.offsetTop, "上偏移量");
        //offsetLeft获取元素的左偏移量从左边到自身的border结束,获取的是数值,相对父元素
        console.log(boxChildren.offsetLeft, "左偏移量");

    </script>
</body>

</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值