Skip to content

Instantly share code, notes, and snippets.

@na0x2c6
Created March 22, 2020 07:21
Show Gist options
  • Save na0x2c6/c4c086ae68e19c1fc64d54ca8b1c8101 to your computer and use it in GitHub Desktop.
Save na0x2c6/c4c086ae68e19c1fc64d54ca8b1c8101 to your computer and use it in GitHub Desktop.
Calculating an element node offset.
function calcOffset(elm: HTMLElement) {
const offset = {
top: 0,
left: 0,
}
do {
offset.top += elm.offsetTop || 0;
offset.left += elm.offsetLeft || 0;
} while((elm = elm.offsetParent as HTMLElement));
return offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment