Skip to content

Commit c45f609

Browse files
committed
Keep track of a hiding state for toggle based animations - Fixes #8685
Closes jquerygh-1018
1 parent 781a5c0 commit c45f609

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/effects.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ jQuery.Animation = jQuery.extend( Animation, {
234234

235235
function defaultPrefilter( elem, props, opts ) {
236236
/*jshint validthis:true */
237-
var index, prop, value, length, dataShow, tween, hooks, oldfire,
237+
var index, prop, value, length, dataShow, toggle, tween, hooks, oldfire,
238238
anim = this,
239239
style = elem.style,
240240
orig = {},
@@ -308,6 +308,7 @@ function defaultPrefilter( elem, props, opts ) {
308308
value = props[ index ];
309309
if ( rfxtypes.exec( value ) ) {
310310
delete props[ index ];
311+
toggle = toggle || value === "toggle";
311312
if ( value === ( hidden ? "hide" : "show" ) ) {
312313
continue;
313314
}
@@ -318,6 +319,14 @@ function defaultPrefilter( elem, props, opts ) {
318319
length = handled.length;
319320
if ( length ) {
320321
dataShow = jQuery._data( elem, "fxshow" ) || jQuery._data( elem, "fxshow", {} );
322+
if ( "hidden" in dataShow ) {
323+
hidden = dataShow.hidden;
324+
}
325+
326+
// store state if its toggle - enables .stop().toggle() to "reverse"
327+
if ( toggle ) {
328+
dataShow.hidden = !hidden;
329+
}
321330
if ( hidden ) {
322331
jQuery( elem ).show();
323332
} else {

test/unit/effects.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,4 +1865,48 @@ test( "Animations with 0 duration don't ease (#12273)", 1, function() {
18651865
delete jQuery.easing.test;
18661866
});
18671867

1868+
jQuery.map([ "toggle", "slideToggle", "fadeToggle" ], function ( method ) {
1869+
// this test would look a lot better if we were using something to override
1870+
// the default timers
1871+
asyncTest( "toggle state tests: " + method + " (#8685)", function() {
1872+
function secondToggle() {
1873+
var stopped = parseFloat( element.css( check ) );
1874+
tested = false;
1875+
element[ method ]({
1876+
duration: 5000,
1877+
step: function( p, fx ) {
1878+
if ( fx.pos > 0.1 && fx.prop === check && !tested ) {
1879+
tested = true;
1880+
equal( fx.start, stopped, check + " starts at " + stopped + " where it stopped" );
1881+
equal( fx.end, original, check + " ending value is " + original );
1882+
element.stop();
1883+
}
1884+
},
1885+
always: start
1886+
});
1887+
}
1888+
1889+
var tested,
1890+
original,
1891+
check = method === "slideToggle" ? "height" : "opacity",
1892+
element = jQuery( "#foo" );
1893+
1894+
expect( 4 );
1895+
1896+
element[ method ]({
1897+
duration: 5000,
1898+
step: function( p, fx ) {
1899+
if ( fx.pos > 0.1 && fx.prop === check && !tested ) {
1900+
tested = true;
1901+
original = fx.start;
1902+
equal( fx.start !== 0, true, check + " is starting at " + original + " on first toggle" );
1903+
equal( fx.end, 0, check + " is ending at 0 on first toggle" );
1904+
element.stop();
1905+
}
1906+
},
1907+
always: secondToggle
1908+
});
1909+
});
1910+
});
1911+
18681912
} // if ( jQuery.fx )

0 commit comments

Comments
 (0)