Skip to content

Instantly share code, notes, and snippets.

@Dionid
Last active January 11, 2016 13:59
Show Gist options
  • Save Dionid/0d701835393532fa4741 to your computer and use it in GitHub Desktop.
Save Dionid/0d701835393532fa4741 to your computer and use it in GitHub Desktop.
/*
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