HTML DOM 元素 offsetLeft
示例
获取 "myDIV" 的 offsetLeft 位置
const element = document.getElementById("myDIV");
let pos = element.offsetLeft;
自己动手试一试 »
获取 "myDIV" 的位置
const element = document.getElementById("myDIV");
let pos1= element.offsetTop;
let pos2= element.offsetLeft;
自己动手试一试 »
描述
offsetLeft 属性以像素为单位返回相对于父元素的位置。
返回值包括
- 元素的左侧位置和外边距
- 父元素的左侧内边距、滚动条和边框
offsetLeft 属性是只读的。
教程
offsetParent
所有块级元素都报告相对于 offset 父级的偏移量
- offsetTop
- offsetLeft
- offsetWidth
- offsetHeight
offset 父级是最近的、position 属性不为 static 的祖先元素。
如果不存在 offset 父级,则偏移量相对于文档主体。
另请参阅
语法
返回左侧偏移位置
 element.offsetLeft
返回值
| 类型 | 描述 | 
| Number | 元素的左侧位置,以像素为单位。 | 
浏览器支持
element.offsetLeft 在所有浏览器中都受支持
| Chrome | Edge | Firefox | Safari | Opera | IE | 
| 是 | 是 | 是 | 是 | 是 | 是 | 
 
