aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2009-07-19 13:04:18 +0000
committerJohn Resig <jeresig@gmail.com>2009-07-19 13:04:18 +0000
commita3b8ac413f9c55994922c20b39223ab9ae6d1afa (patch)
treeabdcb0b9568254b939ba95cfe50586ed78d3ced8
parentf0681d98fe1fcb2995eca50a41c1c66407eab97a (diff)
downloadjquery-a3b8ac413f9c55994922c20b39223ab9ae6d1afa.tar.gz
jquery-a3b8ac413f9c55994922c20b39223ab9ae6d1afa.zip
Rewrote .offsetParent() to work against the full jQuery set, added tests. Fixes #4922.
-rw-r--r--src/offset.js12
-rw-r--r--test/unit/offset.js27
2 files changed, 33 insertions, 6 deletions
diff --git a/src/offset.js b/src/offset.js
index dab0c2bfc..6e22ce30e 100644
--- a/src/offset.js
+++ b/src/offset.js
@@ -147,11 +147,13 @@ jQuery.fn.extend({
},
offsetParent: function() {
- var offsetParent = this[0].offsetParent || document.body;
- while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') === 'static') ) {
- offsetParent = offsetParent.offsetParent;
- }
- return jQuery( offsetParent );
+ return this.map(function(){
+ var offsetParent = this.offsetParent || document.body;
+ while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') === 'static') ) {
+ offsetParent = offsetParent.offsetParent;
+ }
+ return offsetParent;
+ });
}
});
diff --git a/test/unit/offset.js b/test/unit/offset.js
index b6c50ff71..f9747decb 100644
--- a/test/unit/offset.js
+++ b/test/unit/offset.js
@@ -145,7 +145,32 @@ testoffset("body", function( jQuery ) {
equals( jQuery('body').offset().left, 1, "jQuery('#body').offset().left" );
});
+test("offsetParent", function(){
+ expect(11);
+ var body = jQuery("body").offsetParent();
+ equals( body.length, 1, "Only one offsetParent found." );
+ equals( body[0], document.body, "The body is its own offsetParent." );
+
+ var header = jQuery("#header").offsetParent();
+ equals( header.length, 1, "Only one offsetParent found." );
+ equals( header[0], document.body, "The body is the offsetParent." );
+
+ var div = jQuery("#nothiddendivchild").offsetParent();
+ equals( div.length, 1, "Only one offsetParent found." );
+ equals( div[0], document.body, "The body is the offsetParent." );
+
+ jQuery("#nothiddendiv").css("position", "relative");
+
+ div = jQuery("#nothiddendivchild").offsetParent();
+ equals( div.length, 1, "Only one offsetParent found." );
+ equals( div[0], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
+
+ div = jQuery("body, #nothiddendivchild").offsetParent();
+ equals( div.length, 2, "Two offsetParent found." );
+ equals( div[0], document.body, "The body is the offsetParent." );
+ equals( div[1], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
+});
function testoffset(name, fn) {
@@ -177,4 +202,4 @@ function testoffset(name, fn) {
iframe.contentWindow.location = src;
return iframe;
}
-} \ No newline at end of file
+}