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