元素偏移量 offset

offset 即元素偏移量,可以动态地得到元素的大小,位置等等


offset 常用的属性有以下几种:

要注意获取的都是距离带有定位的父元素的位置,而且返回值都不带单位

  • element.offsetTop:返回值为距离有定位的父元素的上边距的偏移
  • element.offsetLeft:返回值为距离有定位的父元素的左边距的偏移
  • element.offsetWidth:返回值为自身宽度+padding+border,不带单位
  • element.offsetHeight:返回值为自身高度+padding+border,不带单位
  • element.offsetParent:返回值为带有定位的父级元素,如果父级都没有定位,则返回body

 一:element.offsetTop:

返回值为距离有定位的父元素的上边距的偏移,如果父亲没有设置定位,则输出距离为距离body的上边框的距离,如果有定位则为距离有定位的父元素的上边距的偏移

1.对于没有设置定位的父元素,则返回值为距离body上边框的偏移

 <style>
    .father{
      margin: 100px;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }

    .son{
      width: 100px;
      height: 100px;
      margin-top: 45px;
      margin-left: 45px;
      background-color: rgb(230, 227, 227);

    }

    .father::before,
    .father::after{
      content: '';
      display: table;
    }
    .father::after{
      clear: both;
    }
    </style>
</head>
<body>
    <div class="father">
      <div class="son"></div>
    </div>
  <script>
     var father=document.querySelector('.father');
     var son=document.querySelector('.son');
    console.log(son.offsetTop);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 2.如果有设置了定位的父元素,返回值为距离设置了定位的父元素的上边距的偏移

 <style>
    .father{
      position: relative;
      margin: 100px;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }

    .son{
      width: 100px;
      height: 100px;
      margin-top: 45px;
      margin-left: 45px;
      background-color: rgb(230, 227, 227);

    }

    .father::before,
    .father::after{
      content: '';
      display: table;
    }
    .father::after{
      clear: both;
    }
    </style>
</head>
<body>
    <div class="father">
      <div class="son"></div>
    </div>
  <script>
     var father=document.querySelector('.father');
     var son=document.querySelector('.son');
    console.log(son.offsetTop);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 


 一:element.offsetLeft:

返回值为距离有定位的父元素的左边距的偏移,如果父亲没有设置定位,则输出距离为距离body的左边框的距离,如果有定位则为距离有定位的父元素的左边距的偏移

1.对于没有设置定位的父元素,则返回值为距离body左边框的偏移

<style>
      *{
        margin: 0px;
        padding: 0px;
      }
    .father{
        margin: 100px;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }

    .son{
      width: 100px;
      height: 100px;
      margin: 45px;
      background-color: rgb(230, 227, 227);
    }


    .father::before,
    .father::after{
      content: '';
      display: table;
    }
    .father::after{
      clear: both;
    }
    </style>
</head>
<body>
    <div class="father">
      <div class="son"></div>
    </div>
  <script>
     var father=document.querySelector('.father');
     var son=document.querySelector('.son');
    console.log(son.offsetLeft);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 1.对于有设置了定位的父元素,则返回值为距离该设置了定位父元素左边框的偏移

 <style>
      *{
        margin: 0px;
        padding: 0px;
      }
    .father{
      position: relative;
        margin: 100px;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }

    .son{
      width: 100px;
      height: 100px;
      margin: 45px;
      background-color: rgb(230, 227, 227);
    }


    .father::before,
    .father::after{
      content: '';
      display: table;
    }
    .father::after{
      clear: both;
    }
    </style>
</head>
<body>
    <div class="father">
      <div class="son"></div>
    </div>
  <script>
     var father=document.querySelector('.father');
     var son=document.querySelector('.son');
    console.log(son.offsetLeft);
  </script>
</body>

 watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 


三:element.offsetWidth:

返回值为 自身宽度 + padding + border,不带单位

  <style>
    .father{
        padding: 20px;
        border: 10px solid;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }
    </style>
</head>
<body>
    <div class="father"></div>
  <script>
     var father=document.querySelector('.father');
     console.log(father.offsetWidth);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 


四:element.offsetHeight:

返回值为 自身高度 + padding + border,不带单位

  <style>
    .father{
        padding: 20px;
        border: 10px solid;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }
    </style>
</head>
<body>
    <div class="father"></div>
  <script>
     var father=document.querySelector('.father');
     console.log(father.offsetHeight);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 


 五:element.offsetParent:

返回值为带有定位的父级元素,如果父级都没有定位,则返回body

1.父元素没有设置定位,返回值为 body

 <style>
    .father{
        margin: 100px;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }

    .son{
      width: 100px;
      height: 100px;
      margin: 45px;
      background-color: rgb(230, 227, 227);
    }


    .father::before,
    .father::after{
      content: '';
      display: table;
    }
    .father::after{
      clear: both;
    }
    </style>
</head>
<body>
    <div class="father">
      <div class="son"></div>
    </div>
  <script>
     var father=document.querySelector('.father');
     var son=document.querySelector('.son');
    console.log(son.offsetParent);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16

 1.父元素设置了定位,返回值为该设置了定位的父元素

 <style>
    .father{
      position: relative;
        margin: 100px;
        width: 200px;
        height: 200px;
        background-color: rgb(255, 141, 141);
    }

    .son{
      width: 100px;
      height: 100px;
      margin: 45px;
      background-color: rgb(230, 227, 227);
    }


    .father::before,
    .father::after{
      content: '';
      display: table;
    }
    .father::after{
      clear: both;
    }
    </style>
</head>
<body>
    <div class="father">
      <div class="son"></div>
    </div>
  <script>
     var father=document.querySelector('.father');
     var son=document.querySelector('.son');
    console.log(son.offsetParent);
  </script>
</body>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2h5Y2h6KW_5pyA6L-R5oCO5LmI5qC3,size_20,color_FFFFFF,t_70,g_se,x_16


最后来看一下 offset 和 style 的区别:

offset:

  • 可以获得任意样式表的样式值
  • 得到的值为无单位的数值
  • offsetWidth 为 自身宽度+padding+border
  • (重要)offsetWidth 为只读属性,只能获取无法赋值

style: 

  • 只能获得行内样式表样式值
  • 得到的值为有单位的字符串
  • style.width 为盒子的宽度,不包括 padding 和 border
  • (重要)style.width 为可读写属性,可以获取 可以赋值

 

由此可见:获取值使用 offset ,改变值使用 style 更为合适 

 

  • 13
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡卡西最近怎么样

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值