Home >Web Front-end >JS Tutorial >How to get the left and top offset of an element_javascript skills
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.