Skip to content

Commit 1d35208

Browse files
committed
Use correct window reference in offset to work properly cross-frame. Fixes #6190.
1 parent 36faab4 commit 1d35208

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/offset.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ if ( "getBoundingClientRect" in document.documentElement ) {
1616
return jQuery.offset.bodyOffset( elem );
1717
}
1818

19-
var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement,
20-
clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
21-
top = box.top + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop,
22-
left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
19+
var box = elem.getBoundingClientRect(),
20+
doc = elem.ownerDocument,
21+
body = doc.body,
22+
docElem = doc.documentElement,
23+
win = getWindow(doc),
24+
clientTop = docElem.clientTop || body.clientTop || 0,
25+
clientLeft = docElem.clientLeft || body.clientLeft || 0,
26+
scrollTop = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ),
27+
scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
28+
top = box.top + scrollTop - clientTop,
29+
left = box.left + scrollLeft - clientLeft;
2330

2431
return { top: top, left: left };
2532
};

test/unit/offset.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
module("offset");
22

3+
testoffset("absolute"/* in iframe */, function($, iframe) {
4+
expect(4);
5+
6+
var doc = iframe.document, tests;
7+
8+
// force a scroll value on the main window
9+
// this insures that the results will be wrong
10+
// if the offset method is using the scroll offset
11+
// of the parent window
12+
var forceScroll = jQuery('<div>', { width: 2000, height: 2000 }).appendTo('body');
13+
window.scrollTo(1, 1);
14+
15+
// get offset
16+
tests = [
17+
{ id: '#absolute-1', top: 1, left: 1 }
18+
];
19+
jQuery.each( tests, function() {
20+
equals( jQuery( this.id, doc ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
21+
equals( jQuery( this.id, doc ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
22+
});
23+
24+
25+
// get position
26+
tests = [
27+
{ id: '#absolute-1', top: 0, left: 0 }
28+
];
29+
jQuery.each( tests, function() {
30+
equals( jQuery( this.id, doc ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
31+
equals( jQuery( this.id, doc ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
32+
});
33+
34+
forceScroll.remove();
35+
});
36+
337
testoffset("absolute", function( jQuery ) {
438
expect(144);
539

@@ -306,8 +340,8 @@ testoffset("body", function( jQuery ) {
306340
});
307341

308342
test("Chaining offset(coords) returns jQuery object", function() {
309-
expect(2);
310-
var coords = { top: 1, left: 1 };
343+
expect(2);
344+
var coords = { top: 1, left: 1 };
311345
equals( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" );
312346
equals( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" );
313347
});

0 commit comments

Comments
 (0)