DOM学习实用路线(9)——DOM元素的尺寸获取-offset

元素的尺寸获取




  • offset
    • offsetWidth 可视宽度
    • offsetHeight 可视高度
    • offsetLeft 距离定位父级的left坐标
    • offsetTop 距离定位父级的top坐标

offsetWidth 可视宽度
offsetHeight 可视高度

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            margin: 50px auto;
            position: relative;
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        #div {
            position: absolute;
            left: 100px;
            top: 100px;
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>
<body>
 <div id="box">
     <div id="div"></div>
 </div>   
<script>
{
    let div = document.querySelector("#div");
    console.log(div.offsetWidth, div.offsetHeight);
}

</script>    
</body>
</html>

在这里插入图片描述
在这里插入图片描述


padding上下左右各30,所以宽高增加60.

 #div {
     position: absolute;
     left: 100px;
     top: 100px;
     width: 100px;
     height: 100px;
     padding: 30px;
     background: red;
 }

在这里插入图片描述
在这里插入图片描述


增加border

 #div {
     position: absolute;
     left: 100px;
     top: 100px;
     width: 100px;
     height: 100px;
     padding: 30px;
     border: 5px solid #000;
     background: red;
 }

在这里插入图片描述
在这里插入图片描述


offsetLeft 距离定位父级的left坐标
offsetTop 距离定位父级的top坐标

可视宽高 = width(height) + padding + border
offsetTop/offsetLeft: 元素距离定位父级左上角的距离

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            margin: 50px auto;
            position: relative;
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        #div {
            position: absolute;
            left: 100px;
            top: 100px;
            width: 100px;
            height: 100px;
            padding: 30px;
            border: 5px solid #000;
            background: red;
        }
    </style>
</head>
<body>
 <div id="box">
     <div id="div"></div>
 </div>   
<script>
// offset  可见的、可视的
/*
    offsetWidth
    offsetHeight
    offsetTop
    offsetLeft
*/
{
    let div = document.querySelector("#div");
    console.log(div.offsetWidth,div.offsetHeight);
    console.log(div.offsetLeft,div.offsetTop);
}

</script>    
</body>
</html>

在这里插入图片描述
在这里插入图片描述
我们可以PS再验证一下,offsetLeft: 元素距离定位父级左上角的距离是100px。
在这里插入图片描述
offsetTop: 元素距离定位父级左上角的距离是100px。
在这里插入图片描述


加上margin(上面加50像素)

 #div {
     position: absolute;
     left: 100px;
     top: 100px;
     margin: 50px 0 0 ;
     width: 100px;
     height: 100px;
     padding: 30px;
     border: 5px solid #000;
     background: red;
 }

在这里插入图片描述
我们可以PS再验证一下
在这里插入图片描述


margin 左侧也加上距离-100像素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            margin: 50px auto;
            position: relative;
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }
        #div {
            position: absolute;
            left: 100px;
            top: 100px;
            margin: 50px 0 0 -100px;
            width: 100px;
            height: 100px;
            padding: 30px;
            border: 5px solid #000;
            background: red;
        }
    </style>
</head>
<body>
 <div id="box">
     <div id="div"></div>
 </div>   
<script>
// offset  可见的、可视的
/*
    offsetWidth
    offsetHeight
    offsetTop
    offsetLeft
*/
{
    let div = document.querySelector("#div");
    console.log(div.offsetWidth,div.offsetHeight);
    console.log(div.offsetLeft,div.offsetTop);

    let box = document.querySelector("#box");
    console.log(box.offsetLeft,box.offsetTop);
}

</script>    
</body>
</html>

浏览器变化,box也会变化,box上一级无定位,因此它的定位父级就是父级body。
在这里插入图片描述




(后续待补充)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值