@@ -23,78 +23,92 @@ $.effects.effect.bounce = function(o) {
23
23
props = [ 'position' , 'top' , 'bottom' , 'left' , 'right' ] ,
24
24
// defaults:
25
25
mode = $ . effects . setMode ( el , o . mode || 'effect' ) ,
26
+ showhide = rshowhide . test ( mode ) ,
26
27
direction = o . direction || 'up' ,
27
28
distance = o . distance || 20 ,
28
- times = o . times || 5 ,
29
- speed = ( o . duration || 250 ) ,
29
+ times = o . times || 5 ,
30
+
31
+ // number of internal animations
32
+ anims = times * 2 + showhide ,
33
+ speed = ( o . duration || 250 ) / anims ,
34
+ easing = o . easing ,
35
+
30
36
// utility:
31
37
ref = ( direction == 'up' || direction == 'down' ) ? 'top' : 'left' ,
32
38
motion = ( direction == 'up' || direction == 'left' ) , // true is positive
33
- i , animation , animation1 , animation2 ;
34
-
39
+ i ,
40
+ upAnim ,
41
+ downAnim ,
42
+
43
+ // we will need to re-assemble the queue to stack our animations in place
44
+ queue = el . queue ( ) ,
45
+ queuelen = queue . length ;
46
+
35
47
// Avoid touching opacity to prevent clearType and PNG issues in IE
36
- if ( rshowhide . test ( mode ) ) {
48
+ if ( showhide ) {
37
49
props . push ( 'opacity' ) ;
38
50
}
39
51
40
52
$ . effects . save ( el , props ) ;
41
53
el . show ( ) ;
42
54
$ . effects . createWrapper ( el ) ; // Create Wrapper
43
55
56
+ // default distance for the BIGGEST bounce is the outer Distance / 3
44
57
if ( ! distance ) {
45
58
distance = el [ ref == 'top' ? 'outerHeight' : 'outerWidth' ] ( { margin :true } ) / 3 ;
46
59
}
47
- if ( mode == 'show' ) el . css ( 'opacity' , 0 ) . css ( ref , motion ? - distance : distance ) ; // Shift
48
- if ( mode == 'hide' ) distance = distance / ( times * 2 ) ;
49
- if ( mode != 'hide' ) times -- ;
50
-
51
- // Animate
52
- if ( mode == 'show' ) {
53
- animation = {
54
- opacity : 1
55
- } ;
56
- animation [ ref ] = ( motion ? '+=' : '-=' ) + distance ;
57
- el . animate ( animation , speed / 2 , o . easing ) ;
58
- distance = distance / 2 ;
59
- times -- ;
60
- } ;
61
-
62
- // Bounces
63
- for ( i = 0 ; i < times ; i ++ ) {
64
- animation1 = { } ;
65
- animation2 = { } ;
66
- animation1 [ ref ] = ( motion ? '-=' : '+=' ) + distance ;
67
- animation2 [ ref ] = ( motion ? '+=' : '-=' ) + distance ;
68
- el . animate ( animation1 , speed / 2 , o . easing ) . animate ( animation2 , speed / 2 , o . easing ) ;
69
- distance = ( mode == 'hide' ) ? distance * 2 : distance / 2 ;
60
+
61
+ if ( mode == 'show' ) {
62
+ upAnim = { opacity : 1 } ;
63
+ upAnim [ ref ] = 0 ;
64
+
65
+ // fade and set the initial position if we are showing
66
+ el . css ( 'opacity' , 0 )
67
+ . css ( ref , motion ? - distance * 2 : distance * 2 )
68
+ . animate ( upAnim , speed , easing ) ;
69
+ }
70
+
71
+ // start at the smallest distance if we are hiding
72
+ if ( mode == 'hide' ) {
73
+ distance = distance / ( ( times - 1 ) * 2 ) ;
74
+ }
75
+
76
+ // Bounces up then down (or reversed if motion) -- times * 2 animations happen here
77
+ for ( i = 0 ; i < times ; i ++ ) {
78
+ upAnim = { } ;
79
+ downAnim = { } ;
80
+ upAnim [ ref ] = ( motion ? '-=' : '+=' ) + distance ;
81
+ downAnim [ ref ] = ( motion ? '+=' : '-=' ) + distance ;
82
+ el . animate ( upAnim , speed , easing )
83
+ . animate ( downAnim , speed , easing ,
84
+ ( i == times - 1 ) && ( mode != "hide" ) ? finish : undefined ) ;
85
+
86
+ distance = mode == 'hide' ? distance * 2 : distance / 2 ;
70
87
}
71
88
72
89
// Last Bounce
73
90
if ( mode == 'hide' ) {
74
- animation = {
75
- opacity : 0
76
- } ;
77
- animation [ ref ] = ( motion ? '-=' : '+=' ) + distance ;
78
- el . animate ( animation , speed / 2 , o . easing , function ( ) {
91
+ upAnim = { opacity : 0 } ;
92
+ upAnim [ ref ] = ( motion ? '-=' : '+=' ) + distance ;
93
+
94
+ el . animate ( upAnim , speed , easing , function ( ) {
79
95
el . hide ( ) ;
80
- $ . effects . restore ( el , props ) ;
81
- $ . effects . removeWrapper ( el ) ;
82
- $ . isFunction ( o . complete ) && o . complete . apply ( this , arguments ) ;
96
+ finish ( ) ;
83
97
} ) ;
84
- } else {
85
- animation1 = { } ;
86
- animation2 = { } ;
87
- animation1 [ ref ] = ( motion ? '-=' : '+=' ) + distance ;
88
- animation2 [ ref ] = ( motion ? '+=' : '-=' ) + distance ;
89
- el
90
- . animate ( animation1 , speed / 2 , o . easing )
91
- . animate ( animation2 , speed / 2 , o . easing , function ( ) {
92
- $ . effects . restore ( el , props ) ;
93
- $ . effects . removeWrapper ( el ) ;
94
- $ . isFunction ( o . complete ) && o . complete . apply ( this , arguments ) ;
95
- } ) ;
98
+ }
99
+
100
+ // inject all the animations we just queued to be first in line (after "inprogress")
101
+ if ( queuelen > 1 ) {
102
+ queue . splice . apply ( queue ,
103
+ [ 1 , 0 ] . concat ( queue . splice ( queuelen , anims ) ) ) ;
96
104
}
97
105
el . dequeue ( ) ;
106
+
107
+ function finish ( ) {
108
+ $ . effects . restore ( el , props ) ;
109
+ $ . effects . removeWrapper ( el ) ;
110
+ $ . isFunction ( o . complete ) && o . complete . apply ( el [ 0 ] , arguments ) ;
111
+ }
98
112
} ) ;
99
113
100
114
} ;
0 commit comments