Propiedad offsetLeft del elemento
❮ El Objeto ElementoEjemplo
Obtenga la posición offsetLeft de un elemento <div>:
var testDiv = document.getElementById("test");
document.getElementById("demo").innerHTML = testDiv.offsetLeft;
Definición y uso
La propiedad offsetLeft devuelve la posición izquierda (en píxeles) relativa al lado izquierdo del elemento offsetParent.
El valor devuelto incluye:
- la posición izquierda y el margen del elemento
- el relleno izquierdo, la barra de desplazamiento y el borde del elemento offsetParent
Nota: El elemento offsetParent es el ancestro más cercano que tiene una posición distinta a la estática.
Sugerencia: para devolver la posición superior de un elemento, use la propiedad offsetTop .
Compatibilidad con navegador
Property | |||||
---|---|---|---|---|---|
offsetLeft | Yes | 8.0 | Yes | Yes | Yes |
Sintaxis
Devuelve la posición de compensación izquierda:
object.offsetLeft
Detalles técnicos
Valor por defecto: | sin valor predeterminado |
---|---|
Valor de retorno: | Un número, que representa la posición izquierda del elemento, en píxeles |
Versión DOM: | CSSOM |
Más ejemplos
Ejemplo
Obtenga la posición de un elemento <div>:
var testDiv = document.getElementById("test");
var demoDiv = document.getElementById("demo");
demoDiv.innerHTML = "offsetLeft: " + testDiv.offsetLeft + "<br>offsetTop: " + testDiv.offsetTop;
❮ El Objeto Elemento