Home  >  Article  >  Web Front-end  >  How to get the left and top offset of an element_javascript skills

How to get the left and top offset of an element_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:36:071129browse
 function getElementLeft(element) 
{ 
var actualLeft = element.offsetLeft; 
var current = element.offsetParent; 

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

Get 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; 
}

Get the upper offset of the element;

Use the offsetParent attribute to go back level by level in the Dom hierarchy and total the offsets of each level.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn