Skip to content

Instantly share code, notes, and snippets.

@alexcambose
Created February 7, 2019 08:56
Show Gist options
  • Save alexcambose/f428d171a4bce7943efbae3606d8bdbc to your computer and use it in GitHub Desktop.
Save alexcambose/f428d171a4bce7943efbae3606d8bdbc to your computer and use it in GitHub Desktop.
Offset
const getOffset = (element, horizontal = false) => {
if(!element) return 0;
return getOffset(element.offsetParent, horizontal) + (horizontal ? element.offsetLeft : element.offsetTop);
}
// calling
const someElement = document.getElementById('someElementId');
const X = getOffset(someElement);
const Y = getOffset(someElement, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment