Skip to content

Instantly share code, notes, and snippets.

@fabiomcosta
Created October 8, 2009 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiomcosta/205010 to your computer and use it in GitHub Desktop.
Save fabiomcosta/205010 to your computer and use it in GitHub Desktop.
(function(){
var styleString = Element.getComputedStyle;
function styleNumber(element, style){
return styleString(element, style).toInt() || 0;
};
function isBody(element){
return (/^(?:body|html)$/i).test(element.tagName);
};
Element.implement({
getPosition: function(relative){
if (isBody(this)) return {x: 0, y: 0};
var offset = this.getOffsets(), scroll = this.getScrolls();
var position = {x: offset.x - scroll.x, y: offset.y - scroll.y};
var relativePosition = (relative && (relative = document.id(relative))) ? relative.getPosition() : {x: 0, y: 0};
if(relative){
relativePosition.x += relative.getStyle('border-left-width').toInt();
relativePosition.y += relative.getStyle('border-top-width').toInt();
}
return {x: position.x - relativePosition.x, y: position.y - relativePosition.y};
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment