关于js中的offsetwidth、clientwidth、scrollwidth等一系列属性及其方法一直都傻傻分不清,这里就来总结一下这些方法的用法和含义。
目录
一、clientwidth和clientheigh 、 clienttop和clientleft
二、offsetwidth和offsethight 、 offsettop和offsetleft
三、scrollwidth和scrollheight 、 scrolltop和scrollleft
一、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 :内容层顶部 到 可视区域顶部的距离。
4,scrollleft
scrollleft:内容层左端 到 可视区域左端的距离.
注意: 下面元素属性和元素方法都通过 elem.属性 或 elem.方法 的方式使用,window属性通过 window.属性 的方式使用,document属性则通过document调用。