Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 16:36:07
Original
1181 people have browsed it
 function getElementLeft(element) 
{ 
var actualLeft = element.offsetLeft; 
var current = element.offsetParent; 

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

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; 
}
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template