您好,欢迎访问一九零五行业门户网

JS获取各种宽度、高度的简单介绍_基础知识

有时候项目中会用到用js获取元素位置来定位元素,首先通过图片说明scrollwidth,clientwidth,offsetwidth的关系。
js获取各种宽度、高度的简单介绍:
scrollheight: 获取对象的滚动高度。
scrollleft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrolltop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollwidth:获取对象的滚动宽度
offsetheight:获取对象相对于版面或由父坐标 offsetparent 属性指定的父坐标的高度
offsetleft:获取对象相对于版面或由 offsetparent 属性指定的父坐标的计算左侧位置
offsettop:获取对象相对于版面或由 offsettop 属性指定的父坐标的计算顶端位置
event.clientx 相对文档的水平座标
event.clienty 相对文档的垂直座标
event.offsetx 相对容器的水平坐标
event.offsety 相对容器的垂直坐标
document.documentelement.scrolltop 垂直方向滚动的值
event.clientx+document.documentelement.scrolltop 相对文档的水平座标+垂直方向滚动的量
以上主要指ie之中,firefox差异如下:
ie6.0、ff1.06+:
clientwidth = width + padding
clientheight = height + padding
offsetwidth = width + padding + border
offsetheight = height + padding + border
ie5.0/5.5:
clientwidth = width - border
clientheight = height - border
offsetwidth = width
offsetheight = height
(需要提一下:css中的margin属性,与clientwidth、offsetwidth、clientheight、offsetheight均无关)
offsetwidth (width+padding+border)
假设 obj 为某个 html 控件。
obj.offsettop 指 obj 距离上方或上层控件的位置,整型,单位像素。
obj.offsetleft 指 obj 距离左方或上层控件的位置,整型,单位像素。
obj.offsetwidth 指 obj 控件自身的宽度,整型,单位像素。获取对象可见内容的宽度,不包括滚动条,不包括边框;
obj.offsetheight 指 obj 控件自身的高度,整型,单位像素。
offsetwidth 与 style.width 的区别
一、offsettop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。
二、offsettop 只读,而 style.top 可读写。
三、如果没有给 html 元素指定过 top 样式,则 style.top 返回的是空字符串。
其它类似信息

推荐信息