1
0

offset.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. (function() {
  2. if ( !jQuery.fn.offset ) {
  3. return;
  4. }
  5. var supportsScroll, supportsFixedPosition,
  6. forceScroll = jQuery("<div/>").css({ width: 2000, height: 2000 }),
  7. checkSupport = function() {
  8. // Only run once
  9. checkSupport = false;
  10. var checkFixed = jQuery("<div/>").css({ position: "fixed", top: "20px" }).appendTo("#qunit-fixture");
  11. // Must append to body because #qunit-fixture is hidden and elements inside it don't have a scrollTop
  12. forceScroll.appendTo("body");
  13. window.scrollTo( 200, 200 );
  14. supportsScroll = document.documentElement.scrollTop || document.body.scrollTop;
  15. forceScroll.detach();
  16. supportsFixedPosition = checkFixed[0].offsetTop === 20;
  17. checkFixed.remove();
  18. };
  19. module("offset", { setup: function(){
  20. if ( typeof checkSupport === "function" ) {
  21. checkSupport();
  22. }
  23. // Force a scroll value on the main window to ensure incorrect results
  24. // if offset is using the scroll offset of the parent window
  25. forceScroll.appendTo("body");
  26. window.scrollTo( 1, 1 );
  27. forceScroll.detach();
  28. }, teardown: moduleTeardown });
  29. /*
  30. Closure-compiler will roll static methods off of the jQuery object and so they will
  31. not be passed with the jQuery object across the windows. To differentiate this, the
  32. testIframe callbacks use the "$" symbol to refer to the jQuery object passed from
  33. the iframe window and the "jQuery" symbol is used to access any static methods.
  34. */
  35. test("empty set", function() {
  36. expect(2);
  37. strictEqual( jQuery().offset(), undefined, "offset() returns undefined for empty set (#11962)" );
  38. strictEqual( jQuery().position(), undefined, "position() returns undefined for empty set (#11962)" );
  39. });
  40. test("object without getBoundingClientRect", function() {
  41. expect(2);
  42. // Simulates a browser without gBCR on elements, we just want to return 0,0
  43. var result = jQuery({ ownerDocument: document }).offset();
  44. equal( result.top, 0, "Check top" );
  45. equal( result.left, 0, "Check left" );
  46. });
  47. test("disconnected node", function() {
  48. expect(2);
  49. var result = jQuery( document.createElement("div") ).offset();
  50. equal( result.top, 0, "Check top" );
  51. equal( result.left, 0, "Check left" );
  52. });
  53. testIframe("offset/absolute", "absolute", function($, iframe) {
  54. expect(4);
  55. var doc = iframe.document,
  56. tests;
  57. // get offset
  58. tests = [
  59. { "id": "#absolute-1", "top": 1, "left": 1 }
  60. ];
  61. jQuery.each( tests, function() {
  62. equal( jQuery( this["id"], doc ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset().top" );
  63. equal( jQuery( this["id"], doc ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset().left" );
  64. });
  65. // get position
  66. tests = [
  67. { "id": "#absolute-1", "top": 0, "left": 0 }
  68. ];
  69. jQuery.each( tests, function() {
  70. equal( jQuery( this["id"], doc ).position().top, this["top"], "jQuery('" + this["id"] + "').position().top" );
  71. equal( jQuery( this["id"], doc ).position().left, this["left"], "jQuery('" + this["id"] + "').position().left" );
  72. });
  73. });
  74. testIframe("offset/absolute", "absolute", function( $ ) {
  75. expect(178);
  76. var tests, offset;
  77. // get offset tests
  78. tests = [
  79. { "id": "#absolute-1", "top": 1, "left": 1 },
  80. { "id": "#absolute-1-1", "top": 5, "left": 5 },
  81. { "id": "#absolute-1-1-1", "top": 9, "left": 9 },
  82. { "id": "#absolute-2", "top": 20, "left": 20 }
  83. ];
  84. jQuery.each( tests, function() {
  85. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset().top" );
  86. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset().left" );
  87. });
  88. // get position
  89. tests = [
  90. { "id": "#absolute-1", "top": 0, "left": 0 },
  91. { "id": "#absolute-1-1", "top": 1, "left": 1 },
  92. { "id": "#absolute-1-1-1", "top": 1, "left": 1 },
  93. { "id": "#absolute-2", "top": 19, "left": 19 }
  94. ];
  95. jQuery.each( tests, function() {
  96. equal( $( this["id"] ).position().top, this["top"], "jQuery('" + this["id"] + "').position().top" );
  97. equal( $( this["id"] ).position().left, this["left"], "jQuery('" + this["id"] + "').position().left" );
  98. });
  99. // test #5781
  100. offset = $( "#positionTest" ).offset({ "top": 10, "left": 10 }).offset();
  101. equal( offset.top, 10, "Setting offset on element with position absolute but 'auto' values." );
  102. equal( offset.left, 10, "Setting offset on element with position absolute but 'auto' values." );
  103. // set offset
  104. tests = [
  105. { "id": "#absolute-2", "top": 30, "left": 30 },
  106. { "id": "#absolute-2", "top": 10, "left": 10 },
  107. { "id": "#absolute-2", "top": -1, "left": -1 },
  108. { "id": "#absolute-2", "top": 19, "left": 19 },
  109. { "id": "#absolute-1-1-1", "top": 15, "left": 15 },
  110. { "id": "#absolute-1-1-1", "top": 5, "left": 5 },
  111. { "id": "#absolute-1-1-1", "top": -1, "left": -1 },
  112. { "id": "#absolute-1-1-1", "top": 9, "left": 9 },
  113. { "id": "#absolute-1-1", "top": 10, "left": 10 },
  114. { "id": "#absolute-1-1", "top": 0, "left": 0 },
  115. { "id": "#absolute-1-1", "top": -1, "left": -1 },
  116. { "id": "#absolute-1-1", "top": 5, "left": 5 },
  117. { "id": "#absolute-1", "top": 2, "left": 2 },
  118. { "id": "#absolute-1", "top": 0, "left": 0 },
  119. { "id": "#absolute-1", "top": -1, "left": -1 },
  120. { "id": "#absolute-1", "top": 1, "left": 1 }
  121. ];
  122. jQuery.each( tests, function() {
  123. $( this["id"] ).offset({ "top": this["top"], "left": this["left"] });
  124. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset({ top: " + this["top"] + " })" );
  125. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset({ left: " + this["left"] + " })" );
  126. var top = this["top"], left = this["left"];
  127. $( this["id"] ).offset(function(i, val){
  128. equal( val.top, top, "Verify incoming top position." );
  129. equal( val.left, left, "Verify incoming top position." );
  130. return { "top": top + 1, "left": left + 1 };
  131. });
  132. equal( $( this["id"] ).offset().top, this["top"] + 1, "jQuery('" + this["id"] + "').offset({ top: " + (this["top"] + 1) + " })" );
  133. equal( $( this["id"] ).offset().left, this["left"] + 1, "jQuery('" + this["id"] + "').offset({ left: " + (this["left"] + 1) + " })" );
  134. $( this["id"] )
  135. .offset({ "left": this["left"] + 2 })
  136. .offset({ "top": this["top"] + 2 });
  137. equal( $( this["id"] ).offset().top, this["top"] + 2, "Setting one property at a time." );
  138. equal( $( this["id"] ).offset().left, this["left"] + 2, "Setting one property at a time." );
  139. $( this["id"] ).offset({ "top": this["top"], "left": this["left"], "using": function( props ) {
  140. $( this ).css({
  141. "top": props.top + 1,
  142. "left": props.left + 1
  143. });
  144. }});
  145. equal( $( this["id"] ).offset().top, this["top"] + 1, "jQuery('" + this["id"] + "').offset({ top: " + (this["top"] + 1) + ", using: fn })" );
  146. equal( $( this["id"] ).offset().left, this["left"] + 1, "jQuery('" + this["id"] + "').offset({ left: " + (this["left"] + 1) + ", using: fn })" );
  147. });
  148. });
  149. testIframe("offset/relative", "relative", function( $ ) {
  150. expect(60);
  151. var ie, tests;
  152. // IE is collapsing the top margin of 1px; detect and adjust accordingly
  153. ie = $("#relative-1").offset().top === 6;
  154. // get offset
  155. tests = [
  156. { "id": "#relative-1", "top": ie ? 6 : 7, "left": 7 },
  157. { "id": "#relative-1-1", "top": ie ? 13 : 15, "left": 15 },
  158. { "id": "#relative-2", "top": ie ? 141 : 142, "left": 27 }
  159. ];
  160. jQuery.each( tests, function() {
  161. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset().top" );
  162. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset().left" );
  163. });
  164. // get position
  165. tests = [
  166. { "id": "#relative-1", "top": ie ? 5 : 6, "left": 6 },
  167. { "id": "#relative-1-1", "top": ie ? 4 : 5, "left": 5 },
  168. { "id": "#relative-2", "top": ie ? 140 : 141, "left": 26 }
  169. ];
  170. jQuery.each( tests, function() {
  171. equal( $( this["id"] ).position().top, this["top"], "jQuery('" + this["id"] + "').position().top" );
  172. equal( $( this["id"] ).position().left, this["left"], "jQuery('" + this["id"] + "').position().left" );
  173. });
  174. // set offset
  175. tests = [
  176. { "id": "#relative-2", "top": 200, "left": 50 },
  177. { "id": "#relative-2", "top": 100, "left": 10 },
  178. { "id": "#relative-2", "top": -5, "left": -5 },
  179. { "id": "#relative-2", "top": 142, "left": 27 },
  180. { "id": "#relative-1-1", "top": 100, "left": 100 },
  181. { "id": "#relative-1-1", "top": 5, "left": 5 },
  182. { "id": "#relative-1-1", "top": -1, "left": -1 },
  183. { "id": "#relative-1-1", "top": 15, "left": 15 },
  184. { "id": "#relative-1", "top": 100, "left": 100 },
  185. { "id": "#relative-1", "top": 0, "left": 0 },
  186. { "id": "#relative-1", "top": -1, "left": -1 },
  187. { "id": "#relative-1", "top": 7, "left": 7 }
  188. ];
  189. jQuery.each( tests, function() {
  190. $( this["id"] ).offset({ "top": this["top"], "left": this["left"] });
  191. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset({ top: " + this["top"] + " })" );
  192. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset({ left: " + this["left"] + " })" );
  193. $( this["id"] ).offset({ "top": this["top"], "left": this["left"], "using": function( props ) {
  194. $( this ).css({
  195. "top": props.top + 1,
  196. "left": props.left + 1
  197. });
  198. }});
  199. equal( $( this["id"] ).offset().top, this["top"] + 1, "jQuery('" + this["id"] + "').offset({ top: " + (this["top"] + 1) + ", using: fn })" );
  200. equal( $( this["id"] ).offset().left, this["left"] + 1, "jQuery('" + this["id"] + "').offset({ left: " + (this["left"] + 1) + ", using: fn })" );
  201. });
  202. });
  203. testIframe("offset/static", "static", function( $ ) {
  204. expect( 80 );
  205. var ie, tests;
  206. // IE is collapsing the top margin of 1px; detect and adjust accordingly
  207. ie = $("#static-1").offset().top === 6;
  208. // get offset
  209. tests = [
  210. { "id": "#static-1", "top": ie ? 6 : 7, "left": 7 },
  211. { "id": "#static-1-1", "top": ie ? 13 : 15, "left": 15 },
  212. { "id": "#static-1-1-1", "top": ie ? 20 : 23, "left": 23 },
  213. { "id": "#static-2", "top": ie ? 121 : 122, left: 7 }
  214. ];
  215. jQuery.each( tests, function() {
  216. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset().top" );
  217. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset().left" );
  218. });
  219. // get position
  220. tests = [
  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 }
  225. ];
  226. jQuery.each( tests, function() {
  227. equal( $( this["id"] ).position().top, this["top"], "jQuery('" + this["top"] + "').position().top" );
  228. equal( $( this["id"] ).position().left, this["left"], "jQuery('" + this["left"] +"').position().left" );
  229. });
  230. // set offset
  231. tests = [
  232. { "id": "#static-2", "top": 200, "left": 200 },
  233. { "id": "#static-2", "top": 100, "left": 100 },
  234. { "id": "#static-2", "top": -2, "left": -2 },
  235. { "id": "#static-2", "top": 121, "left": 6 },
  236. { "id": "#static-1-1-1", "top": 50, "left": 50 },
  237. { "id": "#static-1-1-1", "top": 10, "left": 10 },
  238. { "id": "#static-1-1-1", "top": -1, "left": -1 },
  239. { "id": "#static-1-1-1", "top": 22, "left": 22 },
  240. { "id": "#static-1-1", "top": 25, "left": 25 },
  241. { "id": "#static-1-1", "top": 10, "left": 10 },
  242. { "id": "#static-1-1", "top": -3, "left": -3 },
  243. { "id": "#static-1-1", "top": 14, "left": 14 },
  244. { "id": "#static-1", "top": 30, "left": 30 },
  245. { "id": "#static-1", "top": 2, "left": 2 },
  246. { "id": "#static-1", "top": -2, "left": -2 },
  247. { "id": "#static-1", "top": 7, "left": 7 }
  248. ];
  249. jQuery.each( tests, function() {
  250. $( this["id"] ).offset({ "top": this["top"], "left": this["left"] });
  251. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset({ top: " + this["top"] + " })" );
  252. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset({ left: " + this["left"] + " })" );
  253. $( this["id"] ).offset({ "top": this["top"], "left": this["left"], "using": function( props ) {
  254. $( this ).css({
  255. "top": props.top + 1,
  256. "left": props.left + 1
  257. });
  258. }});
  259. equal( $( this["id"] ).offset().top, this["top"] + 1, "jQuery('" + this["id"] + "').offset({ top: " + (this["top"] + 1) + ", using: fn })" );
  260. equal( $( this["id"] ).offset().left, this["left"] + 1, "jQuery('" + this["id"] + "').offset({ left: " + (this["left"] + 1) + ", using: fn })" );
  261. });
  262. });
  263. testIframe("offset/fixed", "fixed", function( $ ) {
  264. expect(34);
  265. var ie, tests, $noTopLeft;
  266. // IE is collapsing the top margin of 1px; detect and adjust accordingly
  267. ie = $("#fixed-1").position().top === 2;
  268. tests = [
  269. {
  270. "id": "#fixed-1",
  271. "offsetTop": 1001,
  272. "offsetLeft": 1001,
  273. "positionTop": ie ? 2 : 0,
  274. "positionLeft": ie ? 2 : 0
  275. },
  276. {
  277. "id": "#fixed-2",
  278. "offsetTop": 1021,
  279. "offsetLeft": 1021,
  280. "positionTop": ie ? 22 : 20,
  281. "positionLeft": ie ? 22 : 20
  282. }
  283. ];
  284. jQuery.each( tests, function() {
  285. if ( !window.supportsScroll ) {
  286. ok( true, "Browser doesn't support scroll position." );
  287. ok( true, "Browser doesn't support scroll position." );
  288. ok( true, "Browser doesn't support scroll position." );
  289. ok( true, "Browser doesn't support scroll position." );
  290. } else if ( window.supportsFixedPosition ) {
  291. equal( $( this["id"] ).offset().top, this["offsetTop"], "jQuery('" + this["id"] + "').offset().top" );
  292. equal( $( this["id"] ).position().top, this["positionTop"], "jQuery('" + this["id"] + "').position().top" );
  293. equal( $( this["id"] ).offset().left, this["offsetLeft"], "jQuery('" + this["id"] + "').offset().left" );
  294. equal( $( this["id"] ).position().left, this["positionLeft"], "jQuery('" + this["id"] + "').position().left" );
  295. } else {
  296. // need to have same number of assertions
  297. ok( true, "Fixed position is not supported" );
  298. ok( true, "Fixed position is not supported" );
  299. ok( true, "Fixed position is not supported" );
  300. ok( true, "Fixed position is not supported" );
  301. }
  302. });
  303. tests = [
  304. { "id": "#fixed-1", "top": 100, "left": 100 },
  305. { "id": "#fixed-1", "top": 0, "left": 0 },
  306. { "id": "#fixed-1", "top": -4, "left": -4 },
  307. { "id": "#fixed-2", "top": 200, "left": 200 },
  308. { "id": "#fixed-2", "top": 0, "left": 0 },
  309. { "id": "#fixed-2", "top": -5, "left": -5 }
  310. ];
  311. jQuery.each( tests, function() {
  312. if ( window.supportsFixedPosition ) {
  313. $( this["id"] ).offset({ "top": this["top"], "left": this["left"] });
  314. equal( $( this["id"] ).offset().top, this["top"], "jQuery('" + this["id"] + "').offset({ top: " + this["top"] + " })" );
  315. equal( $( this["id"] ).offset().left, this["left"], "jQuery('" + this["id"] + "').offset({ left: " + this["left"] + " })" );
  316. $( this["id"] ).offset({ "top": this["top"], "left": this["left"], "using": function( props ) {
  317. $( this ).css({
  318. "top": props.top + 1,
  319. "left": props.left + 1
  320. });
  321. }});
  322. equal( $( this["id"] ).offset().top, this["top"] + 1, "jQuery('" + this["id"] + "').offset({ top: " + (this["top"] + 1) + ", using: fn })" );
  323. equal( $( this["id"] ).offset().left, this["left"] + 1, "jQuery('" + this["id"] + "').offset({ left: " + (this["left"] + 1) + ", using: fn })" );
  324. } else {
  325. // need to have same number of assertions
  326. ok( true, "Fixed position is not supported" );
  327. ok( true, "Fixed position is not supported" );
  328. ok( true, "Fixed position is not supported" );
  329. ok( true, "Fixed position is not supported" );
  330. }
  331. });
  332. // Bug 8316
  333. $noTopLeft = $("#fixed-no-top-left");
  334. if ( window.supportsFixedPosition ) {
  335. equal( $noTopLeft.offset().top, 1007, "Check offset top for fixed element with no top set" );
  336. equal( $noTopLeft.offset().left, 1007, "Check offset left for fixed element with no left set" );
  337. } else {
  338. // need to have same number of assertions
  339. ok( true, "Fixed position is not supported" );
  340. ok( true, "Fixed position is not supported" );
  341. }
  342. });
  343. testIframe("offset/table", "table", function( $ ) {
  344. expect(4);
  345. equal( $("#table-1").offset().top, 6, "jQuery('#table-1').offset().top" );
  346. equal( $("#table-1").offset().left, 6, "jQuery('#table-1').offset().left" );
  347. equal( $("#th-1").offset().top, 10, "jQuery('#th-1').offset().top" );
  348. equal( $("#th-1").offset().left, 10, "jQuery('#th-1').offset().left" );
  349. });
  350. testIframe("offset/scroll", "scroll", function( $, win ) {
  351. expect(24);
  352. // If we're going to bastardize the tests, let's just DO it
  353. var ie = /msie [678]/i.test( navigator.userAgent );
  354. if ( ie ) {
  355. ok( true, "TestSwarm's iframe has hosed this test in oldIE, we surrender" );
  356. } else {
  357. equal( $("#scroll-1").offset().top, 7, "jQuery('#scroll-1').offset().top" );
  358. }
  359. equal( $("#scroll-1").offset().left, 7, "jQuery('#scroll-1').offset().left" );
  360. if ( ie ) {
  361. ok( true, "TestSwarm's iframe has hosed this test in oldIE, we surrender" );
  362. } else {
  363. equal( $("#scroll-1-1").offset().top, 11, "jQuery('#scroll-1-1').offset().top" );
  364. }
  365. equal( $("#scroll-1-1").offset().left, 11, "jQuery('#scroll-1-1').offset().left" );
  366. // scroll offset tests .scrollTop/Left
  367. equal( $("#scroll-1").scrollTop(), 5, "jQuery('#scroll-1').scrollTop()" );
  368. equal( $("#scroll-1").scrollLeft(), 5, "jQuery('#scroll-1').scrollLeft()" );
  369. equal( $("#scroll-1-1").scrollTop(), 0, "jQuery('#scroll-1-1').scrollTop()" );
  370. equal( $("#scroll-1-1").scrollLeft(), 0, "jQuery('#scroll-1-1').scrollLeft()" );
  371. // scroll method chaining
  372. equal( $("#scroll-1").scrollTop(undefined).scrollTop(), 5, ".scrollTop(undefined) is chainable (#5571)" );
  373. equal( $("#scroll-1").scrollLeft(undefined).scrollLeft(), 5, ".scrollLeft(undefined) is chainable (#5571)" );
  374. win.name = "test";
  375. if ( !window.supportsScroll ) {
  376. ok( true, "Browser doesn't support scroll position." );
  377. ok( true, "Browser doesn't support scroll position." );
  378. ok( true, "Browser doesn't support scroll position." );
  379. ok( true, "Browser doesn't support scroll position." );
  380. } else {
  381. equal( $(win).scrollTop(), 1000, "jQuery(window).scrollTop()" );
  382. equal( $(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" );
  383. equal( $(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" );
  384. equal( $(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" );
  385. }
  386. // test jQuery using parent window/document
  387. // jQuery reference here is in the iframe
  388. window.scrollTo(0,0);
  389. equal( $(window).scrollTop(), 0, "jQuery(window).scrollTop() other window" );
  390. equal( $(window).scrollLeft(), 0, "jQuery(window).scrollLeft() other window" );
  391. equal( $(document).scrollTop(), 0, "jQuery(window).scrollTop() other document" );
  392. equal( $(document).scrollLeft(), 0, "jQuery(window).scrollLeft() other document" );
  393. // Tests scrollTop/Left with empty jquery objects
  394. notEqual( $().scrollTop(100), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
  395. notEqual( $().scrollLeft(100), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
  396. notEqual( $().scrollTop(null), null, "jQuery().scrollTop(null) testing setter on empty jquery object" );
  397. notEqual( $().scrollLeft(null), null, "jQuery().scrollLeft(null) testing setter on empty jquery object" );
  398. strictEqual( $().scrollTop(), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
  399. strictEqual( $().scrollLeft(), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
  400. });
  401. testIframe("offset/body", "body", function( $ ) {
  402. expect(4);
  403. equal( $("body").offset().top, 1, "jQuery('#body').offset().top" );
  404. equal( $("body").offset().left, 1, "jQuery('#body').offset().left" );
  405. equal( $("#firstElement").position().left, 5, "$('#firstElement').position().left" );
  406. equal( $("#firstElement").position().top, 5, "$('#firstElement').position().top" );
  407. });
  408. test("chaining", function() {
  409. expect(3);
  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" );
  413. equal( jQuery("#absolute-1").offset(undefined).selector, "#absolute-1", "offset(undefined) returns jQuery object (#5571)" );
  414. });
  415. test("offsetParent", function(){
  416. expect(13);
  417. var body, header, div, area;
  418. body = jQuery("body").offsetParent();
  419. equal( body.length, 1, "Only one offsetParent found." );
  420. equal( body[0], document.documentElement, "The html element is the offsetParent of the body." );
  421. header = jQuery("#qunit").offsetParent();
  422. equal( header.length, 1, "Only one offsetParent found." );
  423. equal( header[0], document.documentElement, "The html element is the offsetParent of #qunit." );
  424. div = jQuery("#nothiddendivchild").offsetParent();
  425. equal( div.length, 1, "Only one offsetParent found." );
  426. equal( div[0], document.getElementById("qunit-fixture"), "The #qunit-fixture is the offsetParent of #nothiddendivchild." );
  427. jQuery("#nothiddendiv").css("position", "relative");
  428. div = jQuery("#nothiddendivchild").offsetParent();
  429. equal( div.length, 1, "Only one offsetParent found." );
  430. equal( div[0], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
  431. div = jQuery("body, #nothiddendivchild").offsetParent();
  432. equal( div.length, 2, "Two offsetParent found." );
  433. equal( div[0], document.documentElement, "The html element is the offsetParent of the body." );
  434. equal( div[1], jQuery("#nothiddendiv")[0], "The div is the offsetParent." );
  435. area = jQuery("#imgmap area").offsetParent();
  436. equal( area[0], document.documentElement, "The html element is the offsetParent of the body." );
  437. div = jQuery("<div>").css({ "position": "absolute" }).appendTo("body");
  438. equal( div.offsetParent()[0], document.documentElement, "Absolutely positioned div returns html as offset parent, see #12139" );
  439. div.remove();
  440. });
  441. test("fractions (see #7730 and #7885)", function() {
  442. expect(2);
  443. jQuery("body").append("<div id='fractions'/>");
  444. var result,
  445. expected = { "top": 1000, "left": 1000 },
  446. div = jQuery("#fractions");
  447. div.css({
  448. "position": "absolute",
  449. "left": "1000.7432222px",
  450. "top": "1000.532325px",
  451. "width": 100,
  452. "height": 100
  453. });
  454. div.offset(expected);
  455. result = div.offset();
  456. equal( result.top, expected.top, "Check top" );
  457. equal( result.left, expected.left, "Check left" );
  458. div.remove();
  459. });
  460. })();