jQuery - recreate slideDown() effect using the animate() function?
Asked Answered
W

1

13

How can I recreate jQuery's $.slideDown effect using the $.animate function?

Withers answered 9/7, 2010 at 20:15 Comment(2)
why not just use slideDown() ?Zonda
because animate() has a step option to which I can attach a function.Withers
P
30

Animate "height", "marginTop", "marginBottom", "paddingTop", and "paddingBottom" to "show".

For example:

$(...).animate({
    "height": "show",
    "marginTop": "show",
    "marginBottom": "show",
    "paddingTop": "show",
    "paddingBottom": "show"
});

Source: jQuery source code.

fxAttrs = [
    // height animations
    [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
    // width animations
    [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
    // opacity animations
    [ "opacity" ]
];
...

jQuery.each({
    slideDown: genFx("show", 1),
    slideUp: genFx("hide", 1),
    slideToggle: genFx("toggle", 1),
    fadeIn: { opacity: "show" },
    fadeOut: { opacity: "hide" }
}, function( name, props ) {
    jQuery.fn[ name ] = function( speed, callback ) {
        return this.animate( props, speed, callback );
    };
});
...

function genFx( type, num ) {
    var obj = {};

    jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
        obj[ this ] = type;
    });

    return obj;
}
Prog answered 9/7, 2010 at 20:20 Comment(1)
Assuming that this would be hide instad of show for .slideUp()?Xylotomous

© 2022 - 2024 — McMap. All rights reserved.