3 var supportsScroll = false;
5 testoffset("absolute"/* in iframe */, function($, iframe) {
8 var doc = iframe.document, tests;
10 // force a scroll value on the main window
11 // this insures that the results will be wrong
12 // if the offset method is using the scroll offset
13 // of the parent window
14 var forceScroll = jQuery('<div>', { width: 2000, height: 2000 }).appendTo('body');
15 window.scrollTo(200, 200);
17 if ( document.documentElement.scrollTop || document.body.scrollTop ) {
18 supportsScroll = true;
21 window.scrollTo(1, 1);
25 { id: '#absolute-1', top: 1, left: 1 }
27 jQuery.each( tests, function() {
28 equals( jQuery( this.id, doc ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
29 equals( jQuery( this.id, doc ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
35 { id: '#absolute-1', top: 0, left: 0 }
37 jQuery.each( tests, function() {
38 equals( jQuery( this.id, doc ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
39 equals( jQuery( this.id, doc ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
45 testoffset("absolute", function( jQuery ) {
50 { id: '#absolute-1', top: 1, left: 1 },
51 { id: '#absolute-1-1', top: 5, left: 5 },
52 { id: '#absolute-1-1-1', top: 9, left: 9 },
53 { id: '#absolute-2', top: 20, left: 20 }
55 jQuery.each( tests, function() {
56 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
57 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
63 { id: '#absolute-1', top: 0, left: 0 },
64 { id: '#absolute-1-1', top: 1, left: 1 },
65 { id: '#absolute-1-1-1', top: 1, left: 1 },
66 { id: '#absolute-2', top: 19, left: 19 }
68 jQuery.each( tests, function() {
69 equals( jQuery( this.id ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
70 equals( jQuery( this.id ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
74 var offset = jQuery( '#positionTest' ).offset({ top: 10, left: 10 }).offset();
75 equals( offset.top, 10, "Setting offset on element with position absolute but 'auto' values." )
76 equals( offset.left, 10, "Setting offset on element with position absolute but 'auto' values." )
81 { id: '#absolute-2', top: 30, left: 30 },
82 { id: '#absolute-2', top: 10, left: 10 },
83 { id: '#absolute-2', top: -1, left: -1 },
84 { id: '#absolute-2', top: 19, left: 19 },
85 { id: '#absolute-1-1-1', top: 15, left: 15 },
86 { id: '#absolute-1-1-1', top: 5, left: 5 },
87 { id: '#absolute-1-1-1', top: -1, left: -1 },
88 { id: '#absolute-1-1-1', top: 9, left: 9 },
89 { id: '#absolute-1-1', top: 10, left: 10 },
90 { id: '#absolute-1-1', top: 0, left: 0 },
91 { id: '#absolute-1-1', top: -1, left: -1 },
92 { id: '#absolute-1-1', top: 5, left: 5 },
93 { id: '#absolute-1', top: 2, left: 2 },
94 { id: '#absolute-1', top: 0, left: 0 },
95 { id: '#absolute-1', top: -1, left: -1 },
96 { id: '#absolute-1', top: 1, left: 1 }
98 jQuery.each( tests, function() {
99 jQuery( this.id ).offset({ top: this.top, left: this.left });
100 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
101 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
103 var top = this.top, left = this.left;
105 jQuery( this.id ).offset(function(i, val){
106 equals( val.top, top, "Verify incoming top position." );
107 equals( val.left, left, "Verify incoming top position." );
108 return { top: top + 1, left: left + 1 };
110 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + " })" );
111 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + " })" );
114 .offset({ left: this.left + 2 })
115 .offset({ top: this.top + 2 });
116 equals( jQuery( this.id ).offset().top, this.top + 2, "Setting one property at a time." );
117 equals( jQuery( this.id ).offset().left, this.left + 2, "Setting one property at a time." );
119 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
125 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
126 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
130 testoffset("relative", function( jQuery ) {
133 // IE is collapsing the top margin of 1px
134 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
138 { id: '#relative-1', top: ie ? 6 : 7, left: 7 },
139 { id: '#relative-1-1', top: ie ? 13 : 15, left: 15 },
140 { id: '#relative-2', top: ie ? 141 : 142, left: 27 }
142 jQuery.each( tests, function() {
143 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
144 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
150 { id: '#relative-1', top: ie ? 5 : 6, left: 6 },
151 { id: '#relative-1-1', top: ie ? 4 : 5, left: 5 },
152 { id: '#relative-2', top: ie ? 140 : 141, left: 26 }
154 jQuery.each( tests, function() {
155 equals( jQuery( this.id ).position().top, this.top, "jQuery('" + this.id + "').position().top" );
156 equals( jQuery( this.id ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
162 { id: '#relative-2', top: 200, left: 50 },
163 { id: '#relative-2', top: 100, left: 10 },
164 { id: '#relative-2', top: -5, left: -5 },
165 { id: '#relative-2', top: 142, left: 27 },
166 { id: '#relative-1-1', top: 100, left: 100 },
167 { id: '#relative-1-1', top: 5, left: 5 },
168 { id: '#relative-1-1', top: -1, left: -1 },
169 { id: '#relative-1-1', top: 15, left: 15 },
170 { id: '#relative-1', top: 100, left: 100 },
171 { id: '#relative-1', top: 0, left: 0 },
172 { id: '#relative-1', top: -1, left: -1 },
173 { id: '#relative-1', top: 7, left: 7 }
175 jQuery.each( tests, function() {
176 jQuery( this.id ).offset({ top: this.top, left: this.left });
177 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
178 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
180 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
186 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
187 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
191 testoffset("static", function( jQuery ) {
194 // IE is collapsing the top margin of 1px
195 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
199 { id: '#static-1', top: ie ? 6 : 7, left: 7 },
200 { id: '#static-1-1', top: ie ? 13 : 15, left: 15 },
201 { id: '#static-1-1-1', top: ie ? 20 : 23, left: 23 },
202 { id: '#static-2', top: ie ? 121 : 122, left: 7 }
204 jQuery.each( tests, function() {
205 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
206 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
212 { id: '#static-1', top: ie ? 5 : 6, left: 6 },
213 { id: '#static-1-1', top: ie ? 12 : 14, left: 14 },
214 { id: '#static-1-1-1', top: ie ? 19 : 22, left: 22 },
215 { id: '#static-2', top: ie ? 120 : 121, left: 6 }
217 jQuery.each( tests, function() {
218 equals( jQuery( this.id ).position().top, this.top, "jQuery('" + this.top + "').position().top" );
219 equals( jQuery( this.id ).position().left, this.left, "jQuery('" + this.left +"').position().left" );
225 { id: '#static-2', top: 200, left: 200 },
226 { id: '#static-2', top: 100, left: 100 },
227 { id: '#static-2', top: -2, left: -2 },
228 { id: '#static-2', top: 121, left: 6 },
229 { id: '#static-1-1-1', top: 50, left: 50 },
230 { id: '#static-1-1-1', top: 10, left: 10 },
231 { id: '#static-1-1-1', top: -1, left: -1 },
232 { id: '#static-1-1-1', top: 22, left: 22 },
233 { id: '#static-1-1', top: 25, left: 25 },
234 { id: '#static-1-1', top: 10, left: 10 },
235 { id: '#static-1-1', top: -3, left: -3 },
236 { id: '#static-1-1', top: 14, left: 14 },
237 { id: '#static-1', top: 30, left: 30 },
238 { id: '#static-1', top: 2, left: 2 },
239 { id: '#static-1', top: -2, left: -2 },
240 { id: '#static-1', top: 7, left: 7 }
242 jQuery.each( tests, function() {
243 jQuery( this.id ).offset({ top: this.top, left: this.left });
244 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
245 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
247 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
253 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
254 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
258 testoffset("fixed", function( jQuery ) {
261 jQuery.offset.initialize();
264 { id: '#fixed-1', top: 1001, left: 1001 },
265 { id: '#fixed-2', top: 1021, left: 1021 }
268 jQuery.each( tests, function() {
269 if ( !supportsScroll ) {
270 ok( true, "Browser doesn't support scroll position." );
271 ok( true, "Browser doesn't support scroll position." );
273 } else if ( jQuery.offset.supportsFixedPosition ) {
274 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
275 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
277 // need to have same number of assertions
278 ok( true, 'Fixed position is not supported' );
279 ok( true, 'Fixed position is not supported' );
284 { id: '#fixed-1', top: 100, left: 100 },
285 { id: '#fixed-1', top: 0, left: 0 },
286 { id: '#fixed-1', top: -4, left: -4 },
287 { id: '#fixed-2', top: 200, left: 200 },
288 { id: '#fixed-2', top: 0, left: 0 },
289 { id: '#fixed-2', top: -5, left: -5 }
292 jQuery.each( tests, function() {
293 if ( jQuery.offset.supportsFixedPosition ) {
294 jQuery( this.id ).offset({ top: this.top, left: this.left });
295 equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
296 equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
298 jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
304 equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + (this.top + 1) + ", using: fn })" );
305 equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + ", using: fn })" );
307 // need to have same number of assertions
308 ok( true, 'Fixed position is not supported' );
309 ok( true, 'Fixed position is not supported' );
310 ok( true, 'Fixed position is not supported' );
311 ok( true, 'Fixed position is not supported' );
316 testoffset("table", function( jQuery ) {
319 equals( jQuery('#table-1').offset().top, 6, "jQuery('#table-1').offset().top" );
320 equals( jQuery('#table-1').offset().left, 6, "jQuery('#table-1').offset().left" );
322 equals( jQuery('#th-1').offset().top, 10, "jQuery('#th-1').offset().top" );
323 equals( jQuery('#th-1').offset().left, 10, "jQuery('#th-1').offset().left" );
326 testoffset("scroll", function( jQuery, win ) {
329 var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
331 // IE is collapsing the top margin of 1px
332 equals( jQuery('#scroll-1').offset().top, ie ? 6 : 7, "jQuery('#scroll-1').offset().top" );
333 equals( jQuery('#scroll-1').offset().left, 7, "jQuery('#scroll-1').offset().left" );
335 // IE is collapsing the top margin of 1px
336 equals( jQuery('#scroll-1-1').offset().top, ie ? 9 : 11, "jQuery('#scroll-1-1').offset().top" );
337 equals( jQuery('#scroll-1-1').offset().left, 11, "jQuery('#scroll-1-1').offset().left" );
340 // scroll offset tests .scrollTop/Left
341 equals( jQuery('#scroll-1').scrollTop(), 5, "jQuery('#scroll-1').scrollTop()" );
342 equals( jQuery('#scroll-1').scrollLeft(), 5, "jQuery('#scroll-1').scrollLeft()" );
344 equals( jQuery('#scroll-1-1').scrollTop(), 0, "jQuery('#scroll-1-1').scrollTop()" );
345 equals( jQuery('#scroll-1-1').scrollLeft(), 0, "jQuery('#scroll-1-1').scrollLeft()" );
347 // equals( jQuery('body').scrollTop(), 0, "jQuery('body').scrollTop()" );
348 // equals( jQuery('body').scrollLeft(), 0, "jQuery('body').scrollTop()" );
352 if ( !supportsScroll ) {
353 ok( true, "Browser doesn't support scroll position." );
354 ok( true, "Browser doesn't support scroll position." );
356 ok( true, "Browser doesn't support scroll position." );
357 ok( true, "Browser doesn't support scroll position." );
359 equals( jQuery(win).scrollTop(), 1000, "jQuery(window).scrollTop()" );
360 equals( jQuery(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" );
362 equals( jQuery(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" );
363 equals( jQuery(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" );
366 // test jQuery using parent window/document
367 // jQuery reference here is in the iframe
368 window.scrollTo(0,0);
369 equals( jQuery(window).scrollTop(), 0, "jQuery(window).scrollTop() other window" );
370 equals( jQuery(window).scrollLeft(), 0, "jQuery(window).scrollLeft() other window" );
371 equals( jQuery(document).scrollTop(), 0, "jQuery(window).scrollTop() other document" );
372 equals( jQuery(document).scrollLeft(), 0, "jQuery(window).scrollLeft() other document" );
375 testoffset("body", function( jQuery ) {
378 equals( jQuery('body').offset().top, 1, "jQuery('#body').offset().top" );
379 equals( jQuery('body').offset().left, 1, "jQuery('#body').offset().left" );
382 test("Chaining offset(coords) returns jQuery object", function() {
384 var coords = { top: 1, left: 1 };
385 equals( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" );
386 equals( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" );
389 test("offsetParent", function(){
392 var body = jQuery("body").offsetParent();
393 equals( body.length, 1, "Only one offsetParent found." );
394 equals( body[0], document.body, "The body is its own offsetParent." );
396 var header = jQuery("#qunit-header").offsetParent();
397 equals( header.length, 1, "Only one offsetParent found." );
398 equals( header[0], document.body, "The body is the offsetParent." );
400 var div = jQuery("#nothiddendivchild").offsetParent();
401 equals( div.length, 1, "Only one offsetParent found." );
402 equals( div[0], document.body, "The body is the offsetParent." );
404 jQuery("#nothiddendiv").css("position", "relative");
406 div = jQuery("#nothiddendivchild").offsetParent();
407 equals( div.length, 1, "Only one offsetParent found." );
408 equals( div[0], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
410 div = jQuery("body, #nothiddendivchild").offsetParent();
411 equals( div.length, 2, "Two offsetParent found." );
412 equals( div[0], document.body, "The body is the offsetParent." );
413 equals( div[1], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
416 function testoffset(name, fn) {
418 test(name, function() {
419 // pause execution for now
422 // load fixture in iframe
423 var iframe = loadFixture(),
424 win = iframe.contentWindow,
425 interval = setInterval( function() {
426 if ( win && win.jQuery && win.jQuery.isReady ) {
427 clearInterval( interval );
430 // call actual tests passing the correct jQuery isntance to use
431 fn.call( this, win.jQuery, win );
432 document.body.removeChild( iframe );
438 function loadFixture() {
439 var src = './data/offset/' + name + '.html?' + parseInt( Math.random()*1000, 10 ),
440 iframe = jQuery('<iframe />').css({
441 width: 500, height: 500, position: 'absolute', top: -600, left: -600, visiblity: 'hidden'
442 }).appendTo('body')[0];
443 iframe.contentWindow.location = src;