@@ -4,6 +4,31 @@ function scrollTopSupport() {
4
4
$ ( window ) . scrollTop ( 1 ) ;
5
5
return $ ( window ) . scrollTop ( ) === 1 ;
6
6
}
7
+ function getScrollbarWidth ( ) {
8
+ var div = $ ( "<div style='display:block;width:50px;height:50px;overflow:hidden;'><div style='height:100px;width:auto;'></div></div>" ) ,
9
+ innerDiv = div . children ( ) [ 0 ] ,
10
+ w1 , w2 ;
11
+ $ ( "body" ) . append ( div ) ;
12
+ w1 = innerDiv . offsetWidth ;
13
+ div . css ( "overflow" , "scroll" ) ;
14
+
15
+ w2 = innerDiv . offsetWidth ;
16
+
17
+ if ( w1 === w2 ) {
18
+ w2 = div [ 0 ] . clientWidth ;
19
+ }
20
+
21
+ div . remove ( ) ;
22
+
23
+ return w1 - w2 ;
24
+ }
25
+ function getScrollInfo ( within ) {
26
+ var that = within [ 0 ] ,
27
+ scrollHeight = within . height ( ) < that . scrollHeight ,
28
+ scrollWidth = within . width ( ) < that . scrollWidth ,
29
+ scrollbarWidth = getScrollbarWidth ( ) ;
30
+ return { height : scrollHeight ? scrollbarWidth : 0 , width : scrollWidth ? scrollbarWidth : 0 } ;
31
+ } ;
7
32
8
33
module ( "position - within" , {
9
34
setup : function ( ) {
@@ -276,7 +301,7 @@ test( "collision: fit, no offset", function() {
276
301
277
302
collisionTest ( {
278
303
collision : "fit"
279
- } , { top : addTop + within . height ( ) - 10 , left : addLeft + within . width ( ) - 10 } , "right bottom" ) ;
304
+ } , { top : addTop + within . height ( ) - 10 - getScrollInfo ( within ) . height , left : addLeft + within . width ( ) - 10 - getScrollInfo ( within ) . width } , "right bottom" ) ;
280
305
281
306
collisionTest2 ( {
282
307
collision : "fit"
@@ -290,7 +315,7 @@ test( "collision: fit, with offset", function() {
290
315
collisionTest ( {
291
316
collision : "fit" ,
292
317
at : "right+2 bottom+3"
293
- } , { top : addTop + within . height ( ) - 10 , left : addLeft + within . width ( ) - 10 } , "right bottom" ) ;
318
+ } , { top : addTop + within . height ( ) - 10 - getScrollInfo ( within ) . height , left : addLeft + within . width ( ) - 10 - getScrollInfo ( within ) . width } , "right bottom" ) ;
294
319
295
320
collisionTest2 ( {
296
321
collision : "fit" ,
@@ -315,7 +340,7 @@ test( "collision: fit, within scrolled", function() {
315
340
collisionTest2 ( {
316
341
collision : "fit" ,
317
342
at : "right+100 bottom+100"
318
- } , { top : addTop + within . height ( ) - 10 , left : addLeft + within . width ( ) - 10 } , "right bottom" ) ;
343
+ } , { top : addTop + within . height ( ) - 10 - getScrollInfo ( within ) . height , left : addLeft + within . width ( ) - 10 - getScrollInfo ( within ) . width } , "right bottom" ) ;
319
344
within . scrollTop ( 0 ) . scrollLeft ( 0 ) ;
320
345
}
321
346
} ) ;
@@ -389,7 +414,7 @@ test( "collision: fit, with margin", function() {
389
414
390
415
collisionTest ( {
391
416
collision : "fit"
392
- } , { top : addTop + within . height ( ) - 20 , left : addLeft + within . width ( ) - 20 } , "right bottom" ) ;
417
+ } , { top : addTop + within . height ( ) - 20 - getScrollInfo ( within ) . height , left : addLeft + within . width ( ) - 20 - getScrollInfo ( within ) . width } , "right bottom" ) ;
393
418
394
419
collisionTest2 ( {
395
420
collision : "fit"
@@ -402,7 +427,7 @@ test( "collision: fit, with margin", function() {
402
427
403
428
collisionTest ( {
404
429
collision : "fit"
405
- } , { top : addTop + within . height ( ) - 20 , left : addLeft + within . width ( ) - 20 } , "right bottom" ) ;
430
+ } , { top : addTop + within . height ( ) - 20 - getScrollInfo ( within ) . height , left : addLeft + within . width ( ) - 20 - getScrollInfo ( within ) . width } , "right bottom" ) ;
406
431
407
432
collisionTest2 ( {
408
433
collision : "fit"
@@ -415,7 +440,7 @@ test( "collision: fit, with margin", function() {
415
440
416
441
collisionTest ( {
417
442
collision : "fit"
418
- } , { top : addTop + within . height ( ) - 25 , left : addLeft + within . width ( ) - 25 } , "right bottom" ) ;
443
+ } , { top : addTop + within . height ( ) - 25 - getScrollInfo ( within ) . height , left : addLeft + within . width ( ) - 25 - getScrollInfo ( within ) . width } , "right bottom" ) ;
419
444
420
445
collisionTest2 ( {
421
446
collision : "fit"
0 commit comments