X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=inline;f=src%2Foffset.js;h=1ee1405850d2eb5ebdd1da1f3434889a4861fe0b;hb=2764127335126601b90aac6488ef927dce1eceda;hp=f25f8336255bd6a084c852dcceefdd09164c1cbe;hpb=a37cccc8827619cfbd76e229af9de2529041ee5b;p=jquery.git
diff --git a/src/offset.js b/src/offset.js
index f25f833..1ee1405 100644
--- a/src/offset.js
+++ b/src/offset.js
@@ -9,12 +9,12 @@ if ( "getBoundingClientRect" in document.documentElement )
left = box.left + (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
return { top: top, left: left };
};
-else
+else
jQuery.fn.offset = function() {
var elem = this[0];
if ( !elem ) return null;
if ( elem === elem.ownerDocument.body ) return jQuery.offset.bodyOffset( elem );
- jQuery.offset.initialized || jQuery.offset.initialize();
+ jQuery.offset.initialize();
var offsetParent = elem.offsetParent, prevOffsetParent = elem,
doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
@@ -23,18 +23,19 @@ else
top = elem.offsetTop, left = elem.offsetLeft;
while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
+ if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) break;
computedStyle = defaultView.getComputedStyle(elem, null);
top -= elem.scrollTop, left -= elem.scrollLeft;
if ( elem === offsetParent ) {
top += elem.offsetTop, left += elem.offsetLeft;
if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.tagName)) )
- top += parseInt( computedStyle.borderTopWidth, 10) || 0,
- left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
+ top += parseFloat( computedStyle.borderTopWidth, 10) || 0,
+ left += parseFloat( computedStyle.borderLeftWidth, 10) || 0;
prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
}
if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" )
- top += parseInt( computedStyle.borderTopWidth, 10) || 0,
- left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
+ top += parseFloat( computedStyle.borderTopWidth, 10) || 0,
+ left += parseFloat( computedStyle.borderLeftWidth, 10) || 0;
prevComputedStyle = computedStyle;
}
@@ -42,7 +43,7 @@ else
top += body.offsetTop,
left += body.offsetLeft;
- if ( prevComputedStyle.position === "fixed" )
+ if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" )
top += Math.max(docElem.scrollTop, body.scrollTop),
left += Math.max(docElem.scrollLeft, body.scrollLeft);
@@ -51,8 +52,7 @@ else
jQuery.offset = {
initialize: function() {
- if ( this.initialized ) return;
- var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, prop, bodyMarginTop = body.style.marginTop,
+ var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, prop, bodyMarginTop = parseFloat(jQuery.curCSS(body, 'marginTop', true), 10) || 0,
html = '
';
jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
@@ -64,23 +64,25 @@ jQuery.offset = {
this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
+ checkDiv.style.position = 'fixed', checkDiv.style.top = '20px';
+ this.supportsFixedPosition = (checkDiv.offsetTop >= 15); // safari subtracts parent border width here which is 5px
+ checkDiv.style.position = '', checkDiv.style.top = '';
+
innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
- body.style.marginTop = '1px';
- this.doesNotIncludeMarginInBodyOffset = (body.offsetTop === 0);
- body.style.marginTop = bodyMarginTop;
+ this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
body.removeChild(container);
- this.initialized = true;
+ jQuery.offset.initialize = function(){};
},
bodyOffset: function(body) {
- jQuery.offset.initialized || jQuery.offset.initialize();
+ jQuery.offset.initialize();
var top = body.offsetTop, left = body.offsetLeft;
if ( jQuery.offset.doesNotIncludeMarginInBodyOffset )
- top += parseInt( jQuery.curCSS(body, 'marginTop', true), 10 ) || 0,
- left += parseInt( jQuery.curCSS(body, 'marginLeft', true), 10 ) || 0;
+ top += parseFloat( jQuery.curCSS(body, 'marginTop', true), 10 ) || 0,
+ left += parseFloat( jQuery.curCSS(body, 'marginLeft', true), 10 ) || 0;
return { top: top, left: left };
}
};
@@ -90,7 +92,7 @@ jQuery.fn.extend({
position: function() {
if ( !this[0] ) return null;
- var left = 0, top = 0, results,
+ var elem = this[0], left = 0, top = 0, results,
// Get *real* offsetParent
offsetParent = this.offsetParent(),
@@ -100,14 +102,14 @@ jQuery.fn.extend({
parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
// Subtract element margins
- // note: when an element has margin: auto the offsetLeft and marginLeft
+ // note: when an element has margin: auto the offsetLeft and marginLeft
// are the same in Safari causing offset.left to incorrectly be 0
- offset.top -= num( this, 'marginTop' );
- offset.left -= num( this, 'marginLeft' );
+ offset.top -= parseFloat( jQuery.curCSS(elem, 'marginTop', true), 10 ) || 0;
+ offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true), 10 ) || 0;
// Add offsetParent borders
- parentOffset.top += num( offsetParent, 'borderTopWidth' );
- parentOffset.left += num( offsetParent, 'borderLeftWidth' );
+ parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth', true), 10 ) || 0;
+ parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true), 10 ) || 0;
// Subtract the two offsets
results = {
@@ -130,7 +132,7 @@ jQuery.fn.extend({
// Create scrollLeft and scrollTop methods
jQuery.each( ['Left', 'Top'], function(i, name) {
var method = 'scroll' + name;
-
+
jQuery.fn[ method ] = function(val) {
if ( !this[0] ) return null;