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 |
是 | 是 | 是 | 是 | 是 | 是 |