You can add two properties to the Element.prototype
to get top/left of any element.
window.Object.defineProperty( Element.prototype, 'documentOffsetTop', {
get: function () { return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop : 0 ); }
} );
window.Object.defineProperty( Element.prototype, 'documentOffsetLeft', {
get: function () { return this.offsetLeft + ( this.offsetParent ? this.offsetParent.documentOffsetLeft : 0 ); }
} );
Here's a demo comparing the results to jQuery's offset().top
and .left
: http://jsfiddle.net/ThinkingStiff/3G7EZ/