|
12 | 12 | */
|
13 | 13 | (function( $, undefined ) {
|
14 | 14 |
|
| 15 | +var rshowhide = /show|hide/; |
| 16 | + |
15 | 17 | $.effects.bounce = function(o) {
|
16 | 18 |
|
17 | 19 | return this.queue(function() {
|
18 | 20 |
|
19 | 21 | // Create element
|
20 |
| - var el = $(this), props = ['position','top','bottom','left','right']; |
| 22 | + var el = $( this ), |
| 23 | + props = [ 'position', 'top', 'bottom', 'left', 'right' ], |
| 24 | + // 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), |
| 30 | + // 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 | + // Avoid touching opacity to prevent clearType and PNG issues in IE |
| 36 | + if ( rshowhide.test( mode ) ) { |
| 37 | + props.push( 'opacity' ); |
| 38 | + } |
21 | 39 |
|
22 |
| - // Set options |
23 |
| - var mode = $.effects.setMode(el, o.options.mode || 'effect'); // Set Mode |
24 |
| - var direction = o.options.direction || 'up'; // Default direction |
25 |
| - var distance = o.options.distance || 20; // Default distance |
26 |
| - var times = o.options.times || 5; // Default # of times |
27 |
| - var speed = o.duration || 250; // Default speed per bounce |
28 |
| - if (/show|hide/.test(mode)) props.push('opacity'); // Avoid touching opacity to prevent clearType and PNG issues in IE |
| 40 | + $.effects.save( el, props ); |
| 41 | + el.show(); |
| 42 | + $.effects.createWrapper( el ); // Create Wrapper |
29 | 43 |
|
30 |
| - // Adjust |
31 |
| - $.effects.save(el, props); el.show(); // Save & Show |
32 |
| - $.effects.createWrapper(el); // Create Wrapper |
33 |
| - var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left'; |
34 |
| - var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg'; |
35 |
| - var distance = o.options.distance || (ref == 'top' ? el.outerHeight({margin:true}) / 3 : el.outerWidth({margin:true}) / 3); |
36 |
| - if (mode == 'show') el.css('opacity', 0).css(ref, motion == 'pos' ? -distance : distance); // Shift |
37 |
| - if (mode == 'hide') distance = distance / (times * 2); |
38 |
| - if (mode != 'hide') times--; |
| 44 | + if ( !distance ) { |
| 45 | + distance = el[ ref == 'top' ? 'outerHeight' : 'outerWidth' ]({ margin:true }) / 3; |
| 46 | + } |
| 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--; |
39 | 50 |
|
40 | 51 | // Animate
|
41 |
| - if (mode == 'show') { // Show Bounce |
42 |
| - var animation = {opacity: 1}; |
43 |
| - animation[ref] = (motion == 'pos' ? '+=' : '-=') + distance; |
44 |
| - el.animate(animation, speed / 2, o.options.easing); |
| 52 | + if ( mode == 'show' ) { |
| 53 | + animation = { |
| 54 | + opacity: 1 |
| 55 | + }; |
| 56 | + animation[ ref ] = ( motion ? '+=' : '-=' ) + distance; |
| 57 | + el.animate( animation, speed / 2, o.easing); |
45 | 58 | distance = distance / 2;
|
46 | 59 | times--;
|
47 | 60 | };
|
48 |
| - for (var i = 0; i < times; i++) { // Bounces |
49 |
| - var animation1 = {}, animation2 = {}; |
50 |
| - animation1[ref] = (motion == 'pos' ? '-=' : '+=') + distance; |
51 |
| - animation2[ref] = (motion == 'pos' ? '+=' : '-=') + distance; |
52 |
| - el.animate(animation1, speed / 2, o.options.easing).animate(animation2, speed / 2, o.options.easing); |
53 |
| - distance = (mode == 'hide') ? distance * 2 : distance / 2; |
54 |
| - }; |
55 |
| - if (mode == 'hide') { // Last Bounce |
56 |
| - var animation = {opacity: 0}; |
57 |
| - animation[ref] = (motion == 'pos' ? '-=' : '+=') + distance; |
58 |
| - el.animate(animation, speed / 2, o.options.easing, function(){ |
59 |
| - el.hide(); // Hide |
60 |
| - $.effects.restore(el, props); $.effects.removeWrapper(el); // Restore |
61 |
| - if(o.callback) o.callback.apply(this, arguments); // Callback |
| 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; |
| 70 | + } |
| 71 | + |
| 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(){ |
| 79 | + el.hide(); |
| 80 | + $.effects.restore( el, props ); |
| 81 | + $.effects.removeWrapper( el ); |
| 82 | + $.isFunction( o.complete ) && o.complete.apply( this, arguments ); |
62 | 83 | });
|
63 | 84 | } else {
|
64 |
| - var animation1 = {}, animation2 = {}; |
65 |
| - animation1[ref] = (motion == 'pos' ? '-=' : '+=') + distance; |
66 |
| - animation2[ref] = (motion == 'pos' ? '+=' : '-=') + distance; |
67 |
| - el.animate(animation1, speed / 2, o.options.easing).animate(animation2, speed / 2, o.options.easing, function(){ |
68 |
| - $.effects.restore(el, props); $.effects.removeWrapper(el); // Restore |
69 |
| - if(o.callback) o.callback.apply(this, arguments); // Callback |
70 |
| - }); |
71 |
| - }; |
72 |
| - el.queue('fx', function() { el.dequeue(); }); |
| 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 | + } |
73 | 97 | el.dequeue();
|
74 | 98 | });
|
75 | 99 |
|
|
0 commit comments