1 module("offset", { teardown: moduleTeardown });
3 test("disconnected node", function() {
6 var result = jQuery( document.createElement("div") ).offset();
8 equal( result.top, 0, "Check top" );
9 equal( result.left, 0, "Check left" );
12 var supportsScroll = false;
14 testoffset("absolute", function($, iframe) {
17 var doc = iframe.document, tests;
19 // force a scroll value on the main window
20 // this insures that the results will be wrong
21 // if the offset method is using the scroll offset
22 // of the parent window
23 var forceScroll = jQuery("<div>", { width: 2000, height: 2000 }).appendTo("body");
24 window.scrollTo(200, 200);
26 if ( document.documentElement.scrollTop || document.body.scrollTop ) {
27 supportsScroll = true;
30 window.scrollTo(1, 1);
34 { id: "#absolute-1", top: 1, left: 1 }
36 jQuery.each( tests, function() {
37 equal( jQuery( this.id, doc ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
38 equal( jQuery( this.id, doc ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
44 { id: "#absolute-1", top: 0, left: 0 }
46 jQuery.each( tests, function() {
47 equal( jQuery( this.id, doc ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
48 equal( jQuery( this.id, doc ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
54 testoffset("absolute", function( jQuery ) {
59 { id: "#absolute-1", top: 1, left: 1 },
60 { id: "#absolute-1-1", top: 5, left: 5 },
61 { id: "#absolute-1-1-1", top: 9, left: 9 },
62 { id: "#absolute-2", top: 20, left: 20 }
64 jQuery.each( tests, function() {
65 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
66 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
72 { id: "#absolute-1", top: 0, left: 0 },
73 { id: "#absolute-1-1", top: 1, left: 1 },
74 { id: "#absolute-1-1-1", top: 1, left: 1 },
75 { id: "#absolute-2", top: 19, left: 19 }
77 jQuery.each( tests, function() {
78 equal( jQuery( this.id ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
79 equal( jQuery( this.id ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
83 var offset = jQuery( "#positionTest" ).offset({ top: 10, left: 10 }).offset();
84 equal( offset.top, 10, "Setting offset on element with position absolute but 'auto' values." )
85 equal( offset.left, 10, "Setting offset on element with position absolute but 'auto' values." )
90 { id: "#absolute-2", top: 30, left: 30 },
91 { id: "#absolute-2", top: 10, left: 10 },
92 { id: "#absolute-2", top: -1, left: -1 },
93 { id: "#absolute-2", top: 19, left: 19 },
94 { id: "#absolute-1-1-1", top: 15, left: 15 },
95 { id: "#absolute-1-1-1", top: 5, left: 5 },
96 { id: "#absolute-1-1-1", top: -1, left: -1 },
97 { id: "#absolute-1-1-1", top: 9, left: 9 },
98 { id: "#absolute-1-1", top: 10, left: 10 },
99 { id: "#absolute-1-1", top: 0, left: 0 },
100 { id: "#absolute-1-1", top: -1, left: -1 },
101 { id: "#absolute-1-1", top: 5, left: 5 },
102 { id: "#absolute-1", top: 2, left: 2 },
103 { id: "#absolute-1", top: 0, left: 0 },
104 { id: "#absolute-1", top: -1, left: -1 },
105 { id: "#absolute-1", top: 1, left: 1 }
107 jQuery.each( tests, function() {
108 jQuery( this.id ).offset({ top: this.top, left: this.left });
109 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
110 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
112 var top = this.top, left = this.left;
114 jQuery( this.id ).offset(function(i, val){
115 equal( val.top, top, "Verify incoming top position." );
116 equal( val.left, left, "Verify incoming top position." );
117 return { top: top + 1, left: left + 1 };
119 equal( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + " })" );
120 equal( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + " })" );
123 .offset({ left: this.left + 2 })
124 .offset({ top: this.top + 2 });
125 equal( jQuery( this.id ).offset().top, this.top + 2, "Setting one property at a time." );
126 equal( jQuery( this.id ).offset().left, this.left + 2, "Setting one property at a time." );
128 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
134 equal( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
135 equal( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
139 testoffset("relative", function( jQuery ) {
142 // IE is collapsing the top margin of 1px
143 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
147 { id: "#relative-1", top: ie ? 6 : 7, left: 7 },
148 { id: "#relative-1-1", top: ie ? 13 : 15, left: 15 },
149 { id: "#relative-2", top: ie ? 141 : 142, left: 27 }
151 jQuery.each( tests, function() {
152 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
153 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
159 { id: "#relative-1", top: ie ? 5 : 6, left: 6 },
160 { id: "#relative-1-1", top: ie ? 4 : 5, left: 5 },
161 { id: "#relative-2", top: ie ? 140 : 141, left: 26 }
163 jQuery.each( tests, function() {
164 equal( jQuery( this.id ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
165 equal( jQuery( this.id ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
171 { id: "#relative-2", top: 200, left: 50 },
172 { id: "#relative-2", top: 100, left: 10 },
173 { id: "#relative-2", top: -5, left: -5 },
174 { id: "#relative-2", top: 142, left: 27 },
175 { id: "#relative-1-1", top: 100, left: 100 },
176 { id: "#relative-1-1", top: 5, left: 5 },
177 { id: "#relative-1-1", top: -1, left: -1 },
178 { id: "#relative-1-1", top: 15, left: 15 },
179 { id: "#relative-1", top: 100, left: 100 },
180 { id: "#relative-1", top: 0, left: 0 },
181 { id: "#relative-1", top: -1, left: -1 },
182 { id: "#relative-1", top: 7, left: 7 }
184 jQuery.each( tests, function() {
185 jQuery( this.id ).offset({ top: this.top, left: this.left });
186 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
187 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
189 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
195 equal( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
196 equal( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
200 testoffset("static", function( jQuery ) {
203 // IE is collapsing the top margin of 1px
204 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
208 { id: "#static-1", top: ie ? 6 : 7, left: 7 },
209 { id: "#static-1-1", top: ie ? 13 : 15, left: 15 },
210 { id: "#static-1-1-1", top: ie ? 20 : 23, left: 23 },
211 { id: "#static-2", top: ie ? 121 : 122, left: 7 }
213 jQuery.each( tests, function() {
214 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
215 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
221 { id: "#static-1", top: ie ? 5 : 6, left: 6 },
222 { id: "#static-1-1", top: ie ? 12 : 14, left: 14 },
223 { id: "#static-1-1-1", top: ie ? 19 : 22, left: 22 },
224 { id: "#static-2", top: ie ? 120 : 121, left: 6 }
226 jQuery.each( tests, function() {
227 equal( jQuery( this.id ).position().top, this.top, "jQuery('" + this.top + "').position().top" );
228 equal( jQuery( this.id ).position().left, this.left, "jQuery('" + this.left +"').position().left" );
234 { id: "#static-2", top: 200, left: 200 },
235 { id: "#static-2", top: 100, left: 100 },
236 { id: "#static-2", top: -2, left: -2 },
237 { id: "#static-2", top: 121, left: 6 },
238 { id: "#static-1-1-1", top: 50, left: 50 },
239 { id: "#static-1-1-1", top: 10, left: 10 },
240 { id: "#static-1-1-1", top: -1, left: -1 },
241 { id: "#static-1-1-1", top: 22, left: 22 },
242 { id: "#static-1-1", top: 25, left: 25 },
243 { id: "#static-1-1", top: 10, left: 10 },
244 { id: "#static-1-1", top: -3, left: -3 },
245 { id: "#static-1-1", top: 14, left: 14 },
246 { id: "#static-1", top: 30, left: 30 },
247 { id: "#static-1", top: 2, left: 2 },
248 { id: "#static-1", top: -2, left: -2 },
249 { id: "#static-1", top: 7, left: 7 }
251 jQuery.each( tests, function() {
252 jQuery( this.id ).offset({ top: this.top, left: this.left });
253 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
254 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
256 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
262 equal( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
263 equal( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
267 testoffset("fixed", function( jQuery ) {
271 { id: "#fixed-1", top: 1001, left: 1001 },
272 { id: "#fixed-2", top: 1021, left: 1021 }
275 jQuery.each( tests, function() {
276 if ( !supportsScroll ) {
277 ok( true, "Browser doesn't support scroll position." );
278 ok( true, "Browser doesn't support scroll position." );
280 } else if ( jQuery.offset.supportsFixedPosition ) {
281 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
282 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
284 // need to have same number of assertions
285 ok( true, "Fixed position is not supported" );
286 ok( true, "Fixed position is not supported" );
291 { id: "#fixed-1", top: 100, left: 100 },
292 { id: "#fixed-1", top: 0, left: 0 },
293 { id: "#fixed-1", top: -4, left: -4 },
294 { id: "#fixed-2", top: 200, left: 200 },
295 { id: "#fixed-2", top: 0, left: 0 },
296 { id: "#fixed-2", top: -5, left: -5 }
299 jQuery.each( tests, function() {
300 if ( jQuery.offset.supportsFixedPosition ) {
301 jQuery( this.id ).offset({ top: this.top, left: this.left });
302 equal( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
303 equal( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
305 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
311 equal( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
312 equal( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
314 // need to have same number of assertions
315 ok( true, "Fixed position is not supported" );
316 ok( true, "Fixed position is not supported" );
317 ok( true, "Fixed position is not supported" );
318 ok( true, "Fixed position is not supported" );
323 var $noTopLeft = jQuery("#fixed-no-top-left");
324 if ( jQuery.offset.supportsFixedPosition ) {
325 equal( $noTopLeft.offset().top, 1007, "Check offset top for fixed element with no top set" );
326 equal( $noTopLeft.offset().left, 1007, "Check offset left for fixed element with no left set" );
328 // need to have same number of assertions
329 ok( true, "Fixed position is not supported" );
330 ok( true, "Fixed position is not supported" );
334 testoffset("table", function( jQuery ) {
337 equal( jQuery("#table-1").offset().top, 6, "jQuery('#table-1').offset().top" );
338 equal( jQuery("#table-1").offset().left, 6, "jQuery('#table-1').offset().left" );
340 equal( jQuery("#th-1").offset().top, 10, "jQuery('#th-1').offset().top" );
341 equal( jQuery("#th-1").offset().left, 10, "jQuery('#th-1').offset().left" );
344 testoffset("scroll", function( jQuery, win ) {
347 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
349 // IE is collapsing the top margin of 1px
350 equal( jQuery("#scroll-1").offset().top, ie ? 6 : 7, "jQuery('#scroll-1').offset().top" );
351 equal( jQuery("#scroll-1").offset().left, 7, "jQuery('#scroll-1').offset().left" );
353 // IE is collapsing the top margin of 1px
354 equal( jQuery("#scroll-1-1").offset().top, ie ? 9 : 11, "jQuery('#scroll-1-1').offset().top" );
355 equal( jQuery("#scroll-1-1").offset().left, 11, "jQuery('#scroll-1-1').offset().left" );
358 // scroll offset tests .scrollTop/Left
359 equal( jQuery("#scroll-1").scrollTop(), 5, "jQuery('#scroll-1').scrollTop()" );
360 equal( jQuery("#scroll-1").scrollLeft(), 5, "jQuery('#scroll-1').scrollLeft()" );
362 equal( jQuery("#scroll-1-1").scrollTop(), 0, "jQuery('#scroll-1-1').scrollTop()" );
363 equal( jQuery("#scroll-1-1").scrollLeft(), 0, "jQuery('#scroll-1-1').scrollLeft()" );
365 // equal( jQuery("body").scrollTop(), 0, "jQuery("body").scrollTop()" );
366 // equal( jQuery("body").scrollLeft(), 0, "jQuery("body").scrollTop()" );
370 if ( !supportsScroll ) {
371 ok( true, "Browser doesn't support scroll position." );
372 ok( true, "Browser doesn't support scroll position." );
374 ok( true, "Browser doesn't support scroll position." );
375 ok( true, "Browser doesn't support scroll position." );
377 equal( jQuery(win).scrollTop(), 1000, "jQuery(window).scrollTop()" );
378 equal( jQuery(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" );
380 equal( jQuery(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" );
381 equal( jQuery(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" );
384 // test jQuery using parent window/document
385 // jQuery reference here is in the iframe
386 window.scrollTo(0,0);
387 equal( jQuery(window).scrollTop(), 0, "jQuery(window).scrollTop() other window" );
388 equal( jQuery(window).scrollLeft(), 0, "jQuery(window).scrollLeft() other window" );
389 equal( jQuery(document).scrollTop(), 0, "jQuery(window).scrollTop() other document" );
390 equal( jQuery(document).scrollLeft(), 0, "jQuery(window).scrollLeft() other document" );
392 // Tests scrollTop/Left with empty jquery objects
393 notEqual( jQuery().scrollTop(100), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
394 notEqual( jQuery().scrollLeft(100), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
395 notEqual( jQuery().scrollTop(null), null, "jQuery().scrollTop(null) testing setter on empty jquery object" );
396 notEqual( jQuery().scrollLeft(null), null, "jQuery().scrollLeft(null) testing setter on empty jquery object" );
397 strictEqual( jQuery().scrollTop(), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
398 strictEqual( jQuery().scrollLeft(), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
401 testoffset("body", function( jQuery ) {
404 equal( jQuery("body").offset().top, 1, "jQuery('#body').offset().top" );
405 equal( jQuery("body").offset().left, 1, "jQuery('#body').offset().left" );
408 test("Chaining offset(coords) returns jQuery object", function() {
410 var coords = { top: 1, left: 1 };
411 equal( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" );
412 equal( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" );
415 test("offsetParent", function(){
418 var body = jQuery("body").offsetParent();
419 equal( body.length, 1, "Only one offsetParent found." );
420 equal( body[0], document.body, "The body is its own offsetParent." );
422 var header = jQuery("#qunit-header").offsetParent();
423 equal( header.length, 1, "Only one offsetParent found." );
424 equal( header[0], document.body, "The body is the offsetParent." );
426 var div = jQuery("#nothiddendivchild").offsetParent();
427 equal( div.length, 1, "Only one offsetParent found." );
428 equal( div[0], document.body, "The body is the offsetParent." );
430 jQuery("#nothiddendiv").css("position", "relative");
432 div = jQuery("#nothiddendivchild").offsetParent();
433 equal( div.length, 1, "Only one offsetParent found." );
434 equal( div[0], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
436 div = jQuery("body, #nothiddendivchild").offsetParent();
437 equal( div.length, 2, "Two offsetParent found." );
438 equal( div[0], document.body, "The body is the offsetParent." );
439 equal( div[1], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
442 test("fractions (see #7730 and #7885)", function() {
445 jQuery('body').append('<div id="fractions"/>');
447 var expected = { top: 1000, left: 1000 };
448 var div = jQuery('#fractions');
451 position: 'absolute',
452 left: '1000.7432222px',
453 top: '1000.532325px',
458 div.offset(expected);
460 var result = div.offset();
462 equal( result.top, expected.top, "Check top" );
463 equal( result.left, expected.left, "Check left" );
468 function testoffset(name, fn) {
470 test(name, function() {
471 // pause execution for now
474 // load fixture in iframe
475 var iframe = loadFixture(),
476 win = iframe.contentWindow,
477 interval = setInterval( function() {
478 if ( win && win.jQuery && win.jQuery.isReady ) {
479 clearInterval( interval );
482 // call actual tests passing the correct jQuery isntance to use
483 fn.call( this, win.jQuery, win );
484 document.body.removeChild( iframe );
490 function loadFixture() {
491 var src = "./data/offset/" + name + ".html?" + parseInt( Math.random()*1000, 10 ),
492 iframe = jQuery("<iframe />").css({
493 width: 500, height: 500, position: "absolute", top: -600, left: -600, visibility: "hidden"
494 }).appendTo("body")[0];
495 iframe.contentWindow.location = src;