Skip to content

Commit 321fd39

Browse files
committed
Some minor tweaks to the effects, trying to clean up code a bit + style guidance
1 parent cc2342a commit 321fd39

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

ui/jquery.effects.bounce.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,28 @@
1212
*/
1313
(function( $, undefined ) {
1414

15-
var rshowhide = /show|hide/;
16-
1715
$.effects.effect.bounce = function(o) {
1816

19-
return this.queue(function() {
20-
21-
// Create element
17+
return this.queue( function( next ) {
2218
var el = $( this ),
23-
props = [ 'position', 'top', 'bottom', 'left', 'right' ],
19+
props = [ "position", "top", "bottom", "left", "right" ],
20+
2421
// defaults:
25-
mode = $.effects.setMode( el, o.mode || 'effect' ),
26-
showhide = rshowhide.test( mode ),
27-
direction = o.direction || 'up',
22+
mode = $.effects.setMode( el, o.mode || "effect" ),
23+
hide = mode === "hide",
24+
show = mode === "show",
25+
direction = o.direction || "up",
2826
distance = o.distance || 20,
2927
times = o.times || 5,
3028

3129
// number of internal animations
32-
anims = times * 2 + showhide,
30+
anims = times * 2 + ( show || hide ? 1 : 0 ),
3331
speed = o.duration / anims,
3432
easing = o.easing,
3533

3634
// utility:
37-
ref = ( direction == 'up' || direction == 'down' ) ? 'top' : 'left',
38-
motion = ( direction == 'up' || direction == 'left' ), // true is positive
35+
ref = ( direction === "up" || direction === "down" ) ? "top" : "left",
36+
motion = ( direction === "up" || direction === "left" ),
3937
i,
4038
upAnim,
4139
downAnim,
@@ -45,8 +43,8 @@ $.effects.effect.bounce = function(o) {
4543
queuelen = queue.length;
4644

4745
// Avoid touching opacity to prevent clearType and PNG issues in IE
48-
if ( showhide ) {
49-
props.push( 'opacity' );
46+
if ( show || hide ) {
47+
props.push( "opacity" );
5048
}
5149

5250
$.effects.save( el, props );
@@ -55,60 +53,66 @@ $.effects.effect.bounce = function(o) {
5553

5654
// default distance for the BIGGEST bounce is the outer Distance / 3
5755
if ( !distance ) {
58-
distance = el[ ref == 'top' ? 'outerHeight' : 'outerWidth' ]({ margin:true }) / 3;
56+
distance = el[ ref === "top" ? "outerHeight" : "outerWidth" ]({ margin:true }) / 3;
5957
}
6058

61-
if ( mode == 'show' ) {
62-
upAnim = { opacity: 1 };
63-
upAnim[ ref ] = 0;
59+
if ( show ) {
60+
downAnim = { opacity: 1 };
61+
downAnim[ ref ] = 0;
6462

65-
// fade and set the initial position if we are showing
66-
el.css( 'opacity', 0 )
63+
// if we are showing, force opacity 0 and set the initial position
64+
// then do the "first" animation
65+
el.css( "opacity", 0 )
6766
.css( ref, motion ? -distance*2 : distance*2 )
68-
.animate( upAnim, speed, easing );
67+
.animate( downAnim, speed, easing );
6968
}
7069

7170
// start at the smallest distance if we are hiding
72-
if ( mode == 'hide' ) {
71+
if ( hide ) {
7372
distance = distance / ( ( times - 1 ) * 2 );
7473
}
7574

76-
// Bounces up then down (or reversed if motion) -- times * 2 animations happen here
75+
downAnim = {};
76+
downAnim[ ref ] = 0;
77+
// Bounces up/down/left/right then back to 0 -- times * 2 animations happen here
7778
for ( i = 0; i < times; i++ ) {
7879
upAnim = {};
79-
downAnim = {};
80-
upAnim[ ref ] = ( motion ? '-=' : '+=' ) + distance;
81-
downAnim[ ref ] = ( motion ? '+=' : '-=' ) + distance;
80+
upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
81+
82+
// add the finish callback to the last animation if we aren't hiding
8283
el.animate( upAnim, speed, easing )
8384
.animate( downAnim, speed, easing,
84-
( i == times - 1 ) && ( mode != "hide" ) ? finish : undefined );
85+
( ( i === times - 1 ) && !hide ) ? finish : undefined );
8586

86-
distance = mode == 'hide' ? distance * 2 : distance / 2;
87+
distance = hide ? distance * 2 : distance / 2;
8788
}
8889

89-
// Last Bounce
90-
if ( mode == 'hide' ) {
90+
// Last Bounce when Hiding
91+
if ( hide ) {
9192
upAnim = { opacity: 0 };
92-
upAnim[ ref ] = ( motion ? '-=' : '+=' ) + distance;
93+
upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
9394

9495
el.animate( upAnim, speed, easing, function(){
9596
el.hide();
9697
finish();
9798
});
9899
}
99100

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+
100109
// inject all the animations we just queued to be first in line (after "inprogress")
101110
if ( queuelen > 1) {
102111
queue.splice.apply( queue,
103112
[ 1, 0 ].concat( queue.splice( queuelen, anims ) ) );
104113
}
105-
el.dequeue();
114+
next();
106115

107-
function finish() {
108-
$.effects.restore( el, props );
109-
$.effects.removeWrapper( el );
110-
$.isFunction( o.complete ) && o.complete.apply( el[ 0 ], arguments );
111-
}
112116
});
113117

114118
};

ui/jquery.effects.pulsate.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,49 @@
1313
(function( $, undefined ) {
1414

1515
$.effects.effect.pulsate = function( o ) {
16-
return this.queue( function() {
16+
return this.queue( function( next ) {
1717
var elem = $( this ),
18-
mode = $.effects.setMode( elem, o.mode || 'show' ),
18+
mode = $.effects.setMode( elem, o.mode || "show" ),
19+
show = mode === "show" || !elem.is( ":visible" ),
20+
showhide = ( show || mode === "hide" ),
1921

20-
// showing or hiding leave of the "last" time
21-
times = ( ( o.times || 5 ) * 2 ) - ( mode == "show" || mode == "hide" ),
22-
duration = o.duration / times,
23-
show = !elem.is( ":visible" ),
22+
// showing or hiding leaves of the "last" animation
23+
anims = ( ( o.times || 5 ) * 2 ) - ( showhide ? 1 : 0 ),
24+
duration = o.duration / anims,
2425
animateTo = 0,
25-
i,
2626
queue = elem.queue(),
27-
queuelen = queue.length;
27+
queuelen = queue.length,
28+
i;
2829

2930
if ( show ) {
30-
elem.css('opacity', 0).show();
31+
elem.css( "opacity", 0 ).show();
3132
animateTo = 1;
3233
}
3334

34-
for ( i = 0; i < times - 1; i++ ) {
35-
elem.animate({
36-
opacity: animateTo
35+
for ( i = 0; i < anims - 1; i++ ) {
36+
elem.animate({
37+
opacity: animateTo
3738
}, duration, o.easing );
3839
animateTo = 1 - animateTo;
3940
}
4041

41-
elem.animate({
42-
opacity: animateTo
42+
elem.animate({
43+
opacity: animateTo
4344
}, duration, o.easing, function() {
44-
if (animateTo == 0) {
45+
if ( animateTo === 0 ) {
4546
elem.hide();
4647
}
47-
(o.complete && o.complete.apply(this, arguments));
48+
if ( o.complete ) {
49+
o.complete.apply( this );
50+
}
4851
});
4952

53+
// We just queued up "anims" animations, we need to put them next in the queue
5054
if ( queuelen > 1) {
5155
queue.splice.apply( queue,
52-
[ 1, 0 ].concat( queue.splice( queuelen, times ) ) );
56+
[ 1, 0 ].concat( queue.splice( queuelen, anims ) ) );
5357
}
54-
elem.dequeue();
58+
next();
5559
});
5660
};
5761

0 commit comments

Comments
 (0)