Skip to content

Commit f5fdbd0

Browse files
committed
Effects: Reduce size
1 parent 867b24b commit f5fdbd0

File tree

3 files changed

+103
-85
lines changed

3 files changed

+103
-85
lines changed

src/css.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ define([
1212
"./css/adjustCSS",
1313
"./css/addGetHookIf",
1414
"./css/support",
15-
"./data/var/dataPriv",
15+
"./css/showHide",
1616

1717
"./core/init",
1818
"./css/swap",
1919
"./core/ready",
2020
"./selector" // contains
2121
], function( jQuery, pnum, access, rmargin, rcssNum, rnumnonpx, cssExpand, isHidden,
22-
getStyles, curCSS, adjustCSS, addGetHookIf, support, dataPriv ) {
22+
getStyles, curCSS, adjustCSS, addGetHookIf, support, showHide ) {
2323

2424
var
2525
// Swappable if display is none or starts with table
@@ -150,46 +150,6 @@ function getWidthOrHeight( elem, name, extra ) {
150150
) + "px";
151151
}
152152

153-
function showHide( elements, show ) {
154-
var display, elem,
155-
values = [],
156-
index = 0,
157-
length = elements.length;
158-
159-
// Determine new display value for elements that need to change
160-
for ( ; index < length; index++ ) {
161-
elem = elements[ index ];
162-
if ( !elem.style ) {
163-
continue;
164-
}
165-
166-
display = elem.style.display;
167-
if ( show ) {
168-
if ( display === "none" ) {
169-
// Restore a pre-hide() value if we have one
170-
values[ index ] = dataPriv.get( elem, "olddisplay" ) || "";
171-
}
172-
} else {
173-
if ( display !== "none" ) {
174-
values[ index ] = "none";
175-
176-
// Remember the value we're replacing
177-
dataPriv.set( elem, "olddisplay", display );
178-
}
179-
}
180-
}
181-
182-
// Set the display of the elements in a second loop
183-
// to avoid the constant reflow
184-
for ( index = 0; index < length; index++ ) {
185-
if ( values[ index ] != null ) {
186-
elements[ index ].style.display = values[ index ];
187-
}
188-
}
189-
190-
return elements;
191-
}
192-
193153
jQuery.extend({
194154

195155
// Add in style property hooks for overriding the default

src/css/showHide.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
define([
2+
"../data/var/dataPriv"
3+
], function( dataPriv ) {
4+
5+
function showHide( elements, show ) {
6+
var display, elem,
7+
values = [],
8+
index = 0,
9+
length = elements.length;
10+
11+
// Determine new display value for elements that need to change
12+
for ( ; index < length; index++ ) {
13+
elem = elements[ index ];
14+
if ( !elem.style ) {
15+
continue;
16+
}
17+
18+
display = elem.style.display;
19+
if ( show ) {
20+
if ( display === "none" ) {
21+
// Restore a pre-hide() value if we have one
22+
values[ index ] = dataPriv.get( elem, "olddisplay" ) || "";
23+
}
24+
} else {
25+
if ( display !== "none" ) {
26+
values[ index ] = "none";
27+
28+
// Remember the value we're replacing
29+
dataPriv.set( elem, "olddisplay", display );
30+
}
31+
}
32+
}
33+
34+
// Set the display of the elements in a second loop
35+
// to avoid the constant reflow
36+
for ( index = 0; index < length; index++ ) {
37+
if ( values[ index ] != null ) {
38+
elements[ index ].style.display = values[ index ];
39+
}
40+
}
41+
42+
return elements;
43+
}
44+
45+
return showHide;
46+
47+
});

src/effects.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ define([
66
"./css/var/isHidden",
77
"./css/adjustCSS",
88
"./data/var/dataPriv",
9+
"./css/showHide",
910

1011
"./core/init",
1112
"./queue",
@@ -14,7 +15,7 @@ define([
1415
"./manipulation",
1516
"./css",
1617
"./effects/Tween"
17-
], function( jQuery, document, rcssNum, cssExpand, isHidden, adjustCSS, dataPriv ) {
18+
], function( jQuery, document, rcssNum, cssExpand, isHidden, adjustCSS, dataPriv, showHide ) {
1819

1920
var
2021
fxNow, timerId,
@@ -81,8 +82,8 @@ function createTween( value, prop, animation ) {
8182

8283
function defaultPrefilter( elem, props, opts ) {
8384
/* jshint validthis: true */
84-
var prop, value, toggle, tween, hooks, oldfire, display, restoreDisplay,
85-
isBox = "height" in props || "width" in props,
85+
var prop, value, toggle, hooks, oldfire, propTween, restoreDisplay, display,
86+
isBox = "width" in props || "height" in props,
8687
anim = this,
8788
orig = {},
8889
style = elem.style,
@@ -117,7 +118,7 @@ function defaultPrefilter( elem, props, opts ) {
117118
// Detect show/hide animations
118119
for ( prop in props ) {
119120
value = props[ prop ];
120-
if ( rfxtypes.exec( value ) ) {
121+
if ( rfxtypes.test( value ) ) {
121122
delete props[ prop ];
122123
toggle = toggle || value === "toggle";
123124
if ( value === ( hidden ? "hide" : "show" ) ) {
@@ -136,7 +137,13 @@ function defaultPrefilter( elem, props, opts ) {
136137
}
137138
}
138139

139-
// Temporarily restrict "overflow" and "display" styles during box animations
140+
// Bail out if this is a no-op like .hide().hide()
141+
propTween = !jQuery.isEmptyObject( props );
142+
if ( !propTween && jQuery.isEmptyObject( orig ) ) {
143+
return;
144+
}
145+
146+
// Restrict "overflow" and "display" styles during box animations
140147
if ( isBox && elem.nodeType === 1 ) {
141148
// Support: IE 9 - 11
142149
// Record all 3 overflow attributes because IE does not infer the shorthand
@@ -158,20 +165,18 @@ function defaultPrefilter( elem, props, opts ) {
158165
// Animate inline elements as inline-block
159166
if ( display === "inline" || display === "inline-block" && restoreDisplay != null ) {
160167
if ( jQuery.css( elem, "float" ) === "none" ) {
161-
if ( !jQuery.isEmptyObject( props ) ) {
162-
style.display = "inline-block";
163168

164169
// Restore the original display value at the end of pure show/hide animations
165-
} else if ( !jQuery.isEmptyObject( orig ) ) {
170+
if ( !propTween ) {
171+
anim.done(function() {
172+
style.display = restoreDisplay;
173+
});
166174
if ( restoreDisplay == null ) {
167175
display = style.display;
168176
restoreDisplay = display === "none" ? "" : display;
169177
}
170-
style.display = "inline-block";
171-
anim.done(function() {
172-
style.display = restoreDisplay;
173-
});
174178
}
179+
style.display = "inline-block";
175180
}
176181
}
177182
}
@@ -186,43 +191,49 @@ function defaultPrefilter( elem, props, opts ) {
186191
}
187192

188193
// Implement show/hide animations
189-
if ( !jQuery.isEmptyObject( orig ) ) {
190-
if ( dataShow ) {
191-
if ( "hidden" in dataShow ) {
192-
hidden = dataShow.hidden;
194+
propTween = false;
195+
for ( prop in orig ) {
196+
197+
// General show/hide setup for this element animation
198+
if ( !propTween ) {
199+
if ( dataShow ) {
200+
if ( "hidden" in dataShow ) {
201+
hidden = dataShow.hidden;
202+
}
203+
} else {
204+
dataShow = dataPriv.access( elem, "fxshow", { display: restoreDisplay } );
193205
}
194-
} else {
195-
dataShow = dataPriv.access( elem, "fxshow", { display: restoreDisplay } );
196-
}
197206

198-
// Store hidden/visible for toggle so `.stop().toggle()` "reverses"
199-
if ( toggle ) {
200-
dataShow.hidden = !hidden;
201-
}
202-
203-
if ( hidden ) {
204-
jQuery( elem ).show();
205-
}
206-
207-
anim.done(function() {
208-
if ( !hidden ) {
209-
jQuery( elem ).hide();
210-
}
211-
dataPriv.remove( elem, "fxshow" );
212-
for ( prop in orig ) {
213-
jQuery.style( elem, prop, orig[ prop ] );
207+
// Store hidden/visible for toggle so `.stop().toggle()` "reverses"
208+
if ( toggle ) {
209+
dataShow.hidden = !hidden;
214210
}
215-
});
216211

217-
for ( prop in orig ) {
218-
tween = createTween( hidden ? dataShow[ prop ] : 0, prop, anim );
212+
// Show elements before animating them
213+
if ( hidden ) {
214+
showHide( [ elem ], true );
215+
}
219216

220-
if ( !( prop in dataShow ) ) {
221-
dataShow[ prop ] = tween.start;
222-
if ( hidden ) {
223-
tween.end = tween.start;
224-
tween.start = prop === "width" || prop === "height" ? 1 : 0;
217+
/* jshint -W083 */
218+
anim.done(function() {
219+
// The final step of a "hide" animation is actually hiding the element
220+
if ( !hidden ) {
221+
showHide( [ elem ] );
222+
}
223+
dataPriv.remove( elem, "fxshow" );
224+
for ( prop in orig ) {
225+
jQuery.style( elem, prop, orig[ prop ] );
225226
}
227+
});
228+
}
229+
230+
// Per-property setup
231+
propTween = createTween( hidden ? dataShow[ prop ] : 0, prop, anim );
232+
if ( !( prop in dataShow ) ) {
233+
dataShow[ prop ] = propTween.start;
234+
if ( hidden ) {
235+
propTween.end = propTween.start;
236+
propTween.start = prop === "width" || prop === "height" ? 1 : 0;
226237
}
227238
}
228239
}

0 commit comments

Comments
 (0)