3 test("disconnected node", function() {
6 var result = jQuery( document.createElement("div") ).offset();
8 equals( result.top, 0, "Check top" );
9 equals( result.left, 0, "Check left" );
12 var supportsScroll = false;
14 testoffset("absolute"/* in iframe */, 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 equals( jQuery( this.id, doc ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
38 equals( 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 equals( jQuery( this.id, doc ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
48 equals( 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 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
66 equals( 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 equals( jQuery( this.id ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
79 equals( jQuery( this.id ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
83 var offset = jQuery( '#positionTest' ).offset({ top: 10, left: 10 }).offset();
84 equals( offset.top, 10, "Setting offset on element with position absolute but 'auto' values." )
85 equals( 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 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
110 equals( 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 equals( val.top, top, "Verify incoming top position." );
116 equals( val.left, left, "Verify incoming top position." );
117 return { top: top + 1, left: left + 1 };
119 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + " })" );
120 equals( 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 equals( jQuery( this.id ).offset().top, this.top + 2, "Setting one property at a time." );
126 equals( 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 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
135 equals( 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 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
153 equals( 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 equals( jQuery( this.id ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
165 equals( 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 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
187 equals( 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 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
196 equals( 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 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
215 equals( 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 equals( jQuery( this.id ).position().top, this.top, "jQuery('" + this.top + "').position().top" );
228 equals( 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 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
254 equals( 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 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
263 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
267 testoffset("fixed", function( jQuery ) {
270 jQuery.offset.initialize();
273 { id: '#fixed-1', top: 1001, left: 1001 },
274 { id: '#fixed-2', top: 1021, left: 1021 }
277 jQuery.each( tests, function() {
278 if ( !supportsScroll ) {
279 ok( true, "Browser doesn't support scroll position." );
280 ok( true, "Browser doesn't support scroll position." );
282 } else if ( jQuery.offset.supportsFixedPosition ) {
283 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
284 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
286 // need to have same number of assertions
287 ok( true, 'Fixed position is not supported' );
288 ok( true, 'Fixed position is not supported' );
293 { id: '#fixed-1', top: 100, left: 100 },
294 { id: '#fixed-1', top: 0, left: 0 },
295 { id: '#fixed-1', top: -4, left: -4 },
296 { id: '#fixed-2', top: 200, left: 200 },
297 { id: '#fixed-2', top: 0, left: 0 },
298 { id: '#fixed-2', top: -5, left: -5 }
301 jQuery.each( tests, function() {
302 if ( jQuery.offset.supportsFixedPosition ) {
303 jQuery( this.id ).offset({ top: this.top, left: this.left });
304 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
305 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
307 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
313 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
314 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
316 // need to have same number of assertions
317 ok( true, 'Fixed position is not supported' );
318 ok( true, 'Fixed position is not supported' );
319 ok( true, 'Fixed position is not supported' );
320 ok( true, 'Fixed position is not supported' );
325 testoffset("table", function( jQuery ) {
328 equals( jQuery('#table-1').offset().top, 6, "jQuery('#table-1').offset().top" );
329 equals( jQuery('#table-1').offset().left, 6, "jQuery('#table-1').offset().left" );
331 equals( jQuery('#th-1').offset().top, 10, "jQuery('#th-1').offset().top" );
332 equals( jQuery('#th-1').offset().left, 10, "jQuery('#th-1').offset().left" );
335 testoffset("scroll", function( jQuery, win ) {
338 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
340 // IE is collapsing the top margin of 1px
341 equals( jQuery('#scroll-1').offset().top, ie ? 6 : 7, "jQuery('#scroll-1').offset().top" );
342 equals( jQuery('#scroll-1').offset().left, 7, "jQuery('#scroll-1').offset().left" );
344 // IE is collapsing the top margin of 1px
345 equals( jQuery('#scroll-1-1').offset().top, ie ? 9 : 11, "jQuery('#scroll-1-1').offset().top" );
346 equals( jQuery('#scroll-1-1').offset().left, 11, "jQuery('#scroll-1-1').offset().left" );
349 // scroll offset tests .scrollTop/Left
350 equals( jQuery('#scroll-1').scrollTop(), 5, "jQuery('#scroll-1').scrollTop()" );
351 equals( jQuery('#scroll-1').scrollLeft(), 5, "jQuery('#scroll-1').scrollLeft()" );
353 equals( jQuery('#scroll-1-1').scrollTop(), 0, "jQuery('#scroll-1-1').scrollTop()" );
354 equals( jQuery('#scroll-1-1').scrollLeft(), 0, "jQuery('#scroll-1-1').scrollLeft()" );
356 // equals( jQuery('body').scrollTop(), 0, "jQuery('body').scrollTop()" );
357 // equals( jQuery('body').scrollLeft(), 0, "jQuery('body').scrollTop()" );
361 if ( !supportsScroll ) {
362 ok( true, "Browser doesn't support scroll position." );
363 ok( true, "Browser doesn't support scroll position." );
365 ok( true, "Browser doesn't support scroll position." );
366 ok( true, "Browser doesn't support scroll position." );
368 equals( jQuery(win).scrollTop(), 1000, "jQuery(window).scrollTop()" );
369 equals( jQuery(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" );
371 equals( jQuery(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" );
372 equals( jQuery(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" );
375 // test jQuery using parent window/document
376 // jQuery reference here is in the iframe
377 window.scrollTo(0,0);
378 equals( jQuery(window).scrollTop(), 0, "jQuery(window).scrollTop() other window" );
379 equals( jQuery(window).scrollLeft(), 0, "jQuery(window).scrollLeft() other window" );
380 equals( jQuery(document).scrollTop(), 0, "jQuery(window).scrollTop() other document" );
381 equals( jQuery(document).scrollLeft(), 0, "jQuery(window).scrollLeft() other document" );
384 testoffset("body", function( jQuery ) {
387 equals( jQuery('body').offset().top, 1, "jQuery('#body').offset().top" );
388 equals( jQuery('body').offset().left, 1, "jQuery('#body').offset().left" );
391 test("Chaining offset(coords) returns jQuery object", function() {
393 var coords = { top: 1, left: 1 };
394 equals( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" );
395 equals( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" );
398 test("offsetParent", function(){
401 var body = jQuery("body").offsetParent();
402 equals( body.length, 1, "Only one offsetParent found." );
403 equals( body[0], document.body, "The body is its own offsetParent." );
405 var header = jQuery("#qunit-header").offsetParent();
406 equals( header.length, 1, "Only one offsetParent found." );
407 equals( header[0], document.body, "The body is the offsetParent." );
409 var div = jQuery("#nothiddendivchild").offsetParent();
410 equals( div.length, 1, "Only one offsetParent found." );
411 equals( div[0], document.body, "The body is the offsetParent." );
413 jQuery("#nothiddendiv").css("position", "relative");
415 div = jQuery("#nothiddendivchild").offsetParent();
416 equals( div.length, 1, "Only one offsetParent found." );
417 equals( div[0], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
419 div = jQuery("body, #nothiddendivchild").offsetParent();
420 equals( div.length, 2, "Two offsetParent found." );
421 equals( div[0], document.body, "The body is the offsetParent." );
422 equals( div[1], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
425 function testoffset(name, fn) {
427 test(name, function() {
428 // pause execution for now
431 // load fixture in iframe
432 var iframe = loadFixture(),
433 win = iframe.contentWindow,
434 interval = setInterval( function() {
435 if ( win && win.jQuery && win.jQuery.isReady ) {
436 clearInterval( interval );
439 // call actual tests passing the correct jQuery isntance to use
440 fn.call( this, win.jQuery, win );
441 document.body.removeChild( iframe );
447 function loadFixture() {
448 var src = './data/offset/' + name + '.html?' + parseInt( Math.random()*1000, 10 ),
449 iframe = jQuery('<iframe />').css({
450 width: 500, height: 500, position: 'absolute', top: -600, left: -600, visiblity: 'hidden'
451 }).appendTo('body')[0];
452 iframe.contentWindow.location = src;