MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-dev-survey

В процессе перевода.

Свойство для чтения HTMLElement.offsetLeft возвращает смещение в пикселях верхнего левого угла текущего элемента от родительского HTMLElement.offsetParent узла.

Для блочных элементов, offsetTop, offsetLeft, offsetWidth и offsetHeight описывают границы блока элемента относительно offsetParent.

However, for inline-level elements (such as span) that can wrap from one line to the next, offsetTop and offsetLeft describe the positions of the first border box (use Element.getClientRects() to get its width and height), while offsetWidth and offsetHeight describe the dimensions of the bounding border box (use Element.getBoundingClientRect() to get its position). Therefore, a box with the left, top, width and height of offsetLeft, offsetTop, offsetWidth and offsetHeight will not be a bounding box for a span with wrapped text.

Syntax

left = element.offsetLeft;

left is an integer representing the offset to the left in pixels from the closest relatively positioned parent element.

Example

var colorTable = document.getElementById("t1");
var tOLeft = colorTable.offsetLeft;
 
if (tOLeft > 5) {
  // large left offset: do something here
}

This example shows a 'long' sentence that wraps within a div with a blue border, and a red box that one might think should describe the boundaries of the span.

Image:offsetLeft.jpg

<div style="width: 300px; border-color:blue;
  border-style:solid; border-width:1;">
  <span>Short span. </span>
  <span id="long">Long span that wraps within this div.</span>
</div>

<div id="box" style="position: absolute; border-color: red;
  border-width: 1; border-style: solid; z-index: 10">
</div>

<script>
  var box = document.getElementById("box");
  var long = document.getElementById("long");
  box.style.left = long.offsetLeft + document.body.scrollLeft + "px";
  box.style.top = long.offsetTop + document.body.scrollTop + "px";
  box.style.width = long.offsetWidth + "px";
  box.style.height = long.offsetHeight + "px";
</script> 

Specification

Specification Status Comment
CSS Object Model (CSSOM) View Module
Определение 'offsetLeft' в этой спецификации.
Рабочий черновик  

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Да) (Да) ? ? ? ?
Feature Android Android Webview Edge Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? (Да) (Да) ? ? ? ? ? (Да)

In compliance with the specification, this property will return null on Webkit if the element is hidden (the style.display of this element or any ancestor is "none") or if the style.position of the element itself is set to "fixed".

This property will return null on Internet Explorer (9) if the style.position of the element itself is set to "fixed". (Having display:none does not affect this browser.)

See also

Метки документа и участники

 Внесли вклад в эту страницу: kulaeff
 Обновлялась последний раз: kulaeff,