JavaScript中元素偏移量offset系列操作案例代码教程,offset翻译过来就是偏移量,我们使用offset系列相关属性可以动态的得到该元素的位置(偏移)、大小等。- <style>
- div {
- width: 200px;
- height: 200px;
- background-color: pink;
- }
- </style>
- <div class="zidiu">
- </div>
- <script>
- var div = document.querySelector('.zidiu');
- console.log(div.offsetLeft);
- console.log(div.offsetTop);
- console.log(div.offsetWidth);
- console.log(div.offsetHeight);
- </script>
复制代码 输出结果为:
8
1.html:24 8
1.html:25 200
1.html:26 200
这里的8位浏览器自带距离,去除方法,样式里面添加代码:- * {
- margin: 0;
- padding: 0;
- }
复制代码
|
|