javascript中的offsetWidth、clientWidth、innerWidth及相关属性方法

作者:飞翔在蓝天下的蜂鸟 时间:2024-05-10 14:07:17 

关于js中的offsetWidth、clientWidth、scrollWidth等一系列属性及其方法一直都傻傻分不清,这里就来总结一下这些方法的用法和含义。

一、clientWidth和clientHeigh 、 clientTop和clientLeft

1,clientWidth的实际宽度
clientWidth = width+左右padding

2,clientHeigh的实际高度
clientHeigh = height + 上下padding 

3,clientTop的实际宽度
clientTop = boder.top(上边框的宽度)

4,clientLeft的实际宽度
clientLeft = boder.left(左边框的宽度)

二、offsetWidth和offsetHight 、 offsetTop和offsetLeft

1,offsetWidth的实际宽度
offsetWidth = width + 左右padding + 左右boder

2,offsetHeith的实际高度
offsetHeith = height + 上下padding + 上下boder

3,offsetTop实际宽度
offsetTop:当前元素 上边框 外边缘 到 最近的已定位父级(offsetParent) 上边框 内边缘的 距离。如果父级都没有定位,则分别是到body 顶部 和左边的距离

4,offsetLeft实际宽度
offsetLeft:当前元素 左边框 外边缘 到 最近的已定位父级(offsetParent) 左边框 内边缘的            距离。如果父级都没有定位,则分别是到body 顶部 和左边的距离

三、scrollWidth和scrollHeight 、 scrollTop和scrollLeft

1,scrollWidth实际宽度
scrollWidth:获取指定标签内容层的真实宽度(可视区域宽度+被隐藏区域宽度)。

2,scrollHeight的实际高度
scrollHeight:获取指定标签内容层的真实高度(可视区域高度+被隐藏区域高度)

3,scrollTop
scrollTop :内容层顶部 到 可视区域顶部的距离。
实例:var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;持续获取高度的方式:


window.addEventListener('scroll', ()=>{
var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
});

4,scrollLeft
scrollLeft:内容层左端 到 可视区域左端的距离.

注意: 下面元素属性和元素方法都通过 elem.属性 或 elem.方法 的方式使用,window属性通过 window.属性 的方式使用,document属性则通过document调用。


<script>
 /*
  ****** 元素视图属性
  * offsetWidth 水平方向 width + 左右padding + 左右border-width
  * offsetHeight 垂直方向 height + 上下padding + 上下border-width
  *
  * clientWidth 水平方向 width + 左右padding
  * clientHeight 垂直方向 height + 上下padding
  *
  * offsetTop 获取当前元素到 定位父节点 的top方向的距离
  * offsetLeft 获取当前元素到 定位父节点 的left方向的距离
  *
  * scrollWidth 元素内容真实的宽度,内容不超出盒子高度时为盒子的clientWidth
  * scrollHeight 元素内容真实的高度,内容不超出盒子高度时为盒子的clientHeight
  *
  ****** 元素视图属性结束
  *
  ****** Window视图属性(低版本IE浏览器[<IE9]不支持) 【自测包含滚动条,但网络教程都说不包含???】
  * innerWidth 浏览器窗口可视区宽度(不包括浏览器控制台、菜单栏、工具栏)
  * innerHeight 浏览器窗口可视区高度(不包括浏览器控制台、菜单栏、工具栏)
  * ***** Window视图属性结束
  *
  ****** Document文档视图
  * (低版本IE的innerWidth、innerHeight的代替方案)
  * document.documentElement.clientWidth 浏览器窗口可视区宽度(不包括浏览器控制台、菜单栏、工具栏、滚动条)
  * document.documentElement.clientHeight 浏览器窗口可视区高度(不包括浏览器控制台、菜单栏、工具栏、滚动条)
  *
  * document.documentElement.offsetHeight 获取整个文档的高度(包含body的margin)
  * document.body.offsetHeight 获取整个文档的高度(不包含body的margin)
  *
  * document.documentElement.scrollTop 返回文档的滚动top方向的距离(当窗口发生滚动时值改变)
  * document.documentElement.scrollLeft 返回文档的滚动left方向的距离(当窗口发生滚动时值改变)
  ****** Document文档视图结束
  *
  ****** 元素方法
  * 1. getBoundingClientRect() 获取元素到body
  * bottom: 元素底边(包括border)到可视区最顶部的距离
  * left: 元素最左边(不包括border)到可视区最左边的距离
  * right: 元素最右边(包括border)到可视区最左边的距离
  * top: 元素顶边(不包括border)到可视区最顶部的距离
  * height: 元素的offsetHeight
  * width: 元素的offsetWidth
  * x: 元素左上角的x坐标
  * y: 元素左上角的y坐标
  *
  * 2. scrollIntoView() 让元素滚动到可视区
  *
  * ***** 元素方法结束
  *
  */
</script>

上面属性中,关于 window.innerWidth 和 window.innerHeight, 我自己测试的结果值是包含滚动条的,但网上的教程和相关文档都说不包括滚动条,虽然滚动条的宽度不大,对整体影响也不明显,但如果有道友有准确答案的,还请不吝赐教,顺手留个言,谢谢!

来源:https://blog.csdn.net/qq_33036599/article/details/81224346

标签:javascript,offsetWidth,clientWidth,innerWidth
0
投稿

猜你喜欢

  • python爬虫获取百度首页内容教学

    2022-12-16 07:02:00
  • MySQL 表字段属性

    2011-09-10 16:01:01
  • 如何得到数据库中所有表名 表字段及字段中文描述

    2012-01-05 18:56:44
  • 自己用python做的一款超炫酷音乐播放器

    2021-05-26 04:47:35
  • Python利用pyHook实现监听用户鼠标与键盘事件

    2021-09-04 19:59:59
  • 如何恢复MYSQL的ROOT口令

    2024-01-16 15:50:08
  • JavaScript caller与callee属性

    2009-01-19 13:39:00
  • golang elasticsearch Client的使用详解

    2024-05-28 15:24:41
  • Python中使用sklearn进行特征降维的方法

    2021-09-20 06:23:50
  • Web端扫码登录的原理和实现讲解

    2022-07-08 11:40:18
  • 解析php中var_dump,var_export,print_r三个函数的区别

    2023-11-24 12:27:02
  • pytorch实现mnist分类的示例讲解

    2022-03-30 09:17:19
  • 教你如何使用Python快速爬取需要的数据

    2022-04-14 16:14:50
  • 详解Python定时器Timer的使用及示例

    2021-01-25 23:54:34
  • Python api构建tensorrt加速模型的步骤详解

    2022-03-01 17:21:19
  • MySQL 备份还原数据库批处理

    2024-01-16 21:03:52
  • 给在DreamWeaver编写CSS的人一些习惯建议

    2007-12-25 12:10:00
  • 5.PHP的其他功能

    2023-11-14 16:45:42
  • asp.net aspnetpager分页统计时与实际不符的解决办法

    2023-06-30 04:02:32
  • Linux中Oracle数据库备份

    2024-01-18 16:03:57
  • asp之家 网络编程 m.aspxhome.com