Created
August 20, 2012 06:32
-
-
Save dewski/3401569 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
jQuery.fn.offset = function( options ) { | |
if ( arguments.length ) { | |
return options === undefined ? | |
this : | |
this.each(function( i ) { | |
jQuery.offset.setOffset( this, options, i ); | |
}); | |
} | |
var box, docElem, body, win, clientTop, clientLeft, scrollTop, scrollLeft, top, left, | |
elem = this[ 0 ], | |
doc = elem && elem.ownerDocument; | |
if ( !doc ) { | |
return; | |
} | |
if ( (body = doc.body) === elem ) { | |
return jQuery.offset.bodyOffset( elem ); | |
} | |
docElem = doc.documentElement; | |
// Make sure we're not dealing with a disconnected DOM node | |
if ( !jQuery.contains( docElem, elem ) ) { | |
return { top: 0, left: 0 }; | |
} | |
box = elem.getBoundingClientRect(); | |
win = getWindow( doc ); | |
clientTop = docElem.clientTop || body.clientTop || 0; | |
clientLeft = docElem.clientLeft || body.clientLeft || 0; | |
scrollTop = win.pageYOffset || docElem.scrollTop; | |
scrollLeft = win.pageXOffset || docElem.scrollLeft; | |
top = box.top + scrollTop - clientTop; | |
left = box.left + scrollLeft - clientLeft; | |
return { top: top, left: left }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment