JS 的元素偏移量 offset

简介: JS 的元素偏移量 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>




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>





一: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>




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>




三: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>





四: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>





五: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>




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>




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

offset:

可以获得任意样式表的样式值

得到的值为无单位的数值

offsetWidth 为 自身宽度+padding+border

(重要)offsetWidth 为只读属性,只能获取无法赋值

style:

只能获得行内样式表样式值

得到的值为有单位的字符串

style.width 为盒子的宽度,不包括 padding 和 border

(重要)style.width 为可读写属性,可以获取 可以赋值


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


相关文章
|
6月前
|
JavaScript 算法 开发者
如何用JS实现在网页上通过鼠标移动批量选择元素的效果?
本文介绍了类似电脑桌面通过鼠标选择多个图标的实现原理。主要通过监听mousedown、mousemove和mouseup事件,动态调整选择框大小并计算与元素的重叠情况。提供了角重叠和相交重叠的检测方法,并附有示例代码和在线演示链接,方便开发者参考与测试。
209 56
|
11月前
|
JavaScript 前端开发 程序员
前端原生Js批量修改页面元素属性的2个方法
原生 Js 的 getElementsByClassName 和 querySelectorAll 都能获取批量的页面元素,但是它们之间有些细微的差别,稍不注意,就很容易弄错!
237 1
|
7月前
|
移动开发 运维 供应链
通过array.some()实现权限检查、表单验证、库存管理、内容审查和数据处理;js数组元素检查的方法,some()的使用详解,array.some与array.every的区别(附实际应用代码)
array.some()可以用来权限检查、表单验证、库存管理、内容审查和数据处理等数据校验工作,核心在于利用其短路机制,速度更快,节约性能。 博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
7月前
|
供应链 JavaScript 前端开发
通过array.every()实现数据验证、权限检查和一致性检查;js数组元素检查的方法,every()的使用详解,array.some与array.every的区别(附实际应用代码)
array.every()可以用来数据验证、权限检查、一致性检查等数据校验工作,核心在于利用其短路机制,速度更快,节约性能。 博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
11月前
|
JavaScript 前端开发 开发者
.js的dom元素操作
【10月更文挑战第29天】通过灵活运用这些 DOM 元素操作方法,JavaScript 可以实现丰富的网页交互效果,如动态更新页面内容、响应用户操作、创建和删除页面元素等。在实际开发中,开发者可以根据具体的需求和场景,选择合适的 DOM 元素操作方法来实现所需的功能,为用户提供更加流畅和动态的网页体验。
|
12月前
|
移动开发 JavaScript 前端开发
原生js如何获取dom元素的自定义属性
原生js如何获取dom元素的自定义属性
326 4
|
12月前
|
JavaScript
js删除数组中已知下标的元素
js删除数组中已知下标的元素
296 4
|
JavaScript 前端开发 索引
JS 删除数组元素( 5种方法 )
JS 删除数组元素( 5种方法 )
609 1
|
JavaScript 前端开发
JavaScript从二维数组抽取若干元素组成新二维数组
JavaScript从二维数组抽取若干元素组成新二维数组
|
JavaScript 前端开发
JavaScript从二维数组抽取元素组成新数组的三种方法
JavaScript从二维数组抽取元素组成新数组的三种方法

热门文章

最新文章