Last active
April 13, 2016 13:02
-
-
Save branneman/d21a69d0e80fc5a30f9625c05b5c621b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {HTMLElement} element - The element's coordinates to calulate | |
* @param {HTMLElement} relativeElement - An optional relative element, defaults to `document.body` | |
* @returns {{ offsetY: Number, offsetX: Number }} | |
*/ | |
const getElementCoordinates = function(element, relativeElement = document.body) { | |
const relativeRect = relativeElement.getBoundingClientRect(); | |
const elementRect = element.getBoundingClientRect(); | |
const offsetY = elementRect.top - relativeRect.top; | |
const offsetX = elementRect.left - relativeRect.left; | |
return { offsetY, offsetX }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment