Created
October 8, 2009 13:40
-
-
Save fabiomcosta/205010 to your computer and use it in GitHub Desktop.
This file contains 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
(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