Last active
January 11, 2016 13:59
-
-
Save Dionid/0d701835393532fa4741 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
/* | |
Get ellement offset. | |
@constructor | |
@param {node} element - needs node element | |
@return {Object} - Top and left element offsets | |
@returnParams {Number} Object.top - top offset | |
@returnParams {Number} Object.left - left offset | |
*/ | |
var getElementOffset = function (element){ | |
if (!(element instanceof HTMLElement)){ | |
return 'Value is not node!' | |
} | |
var de = document.documentElement, | |
box = element.getBoundingClientRect(), | |
top = box.top + window.pageYOffset - de.clientTop, | |
left = box.left + window.pageXOffset - de.clientLeft; | |
if(isNumeric(top) && isNumeric(left)){ | |
return { top: top, left: left }; | |
} else { | |
return 'Problem with values' | |
} | |
} | |
// Helper | |
function isNumeric(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment