Gets the left and upper offsets of an element

  • 2020-03-30 03:57:56
  • OfStack


 function getElementLeft(element) 
{ 
var actualLeft = element.offsetLeft; 
var current = element.offsetParent; 

while (current!==null) 
{ 
actualLeft += current.offsetLeft; 
current = current.offsetParent; 
} 
return actualLeft; 
}

Gets the left offset of the element;


function getElementTop(element) 
{ 
var actualTop = element.offsetTop; 
var current = element.offsetParent; 

while (current!==null) 
{ 
<span style="white-space:pre"> </span>actualTop += current.offsetTop; 
current = current.offsetParent; 
} 
return actualTop; 
}

Gets the upper offset of the element;

Use the offsetParent attribute to trace up through the Dom hierarchy, sumping offsets for each layer.


Related articles: