How does slideUp() work in jQuery? I'm trying to make my own slideRIght()

天涯浪子 提交于 2019-12-23 07:37:12

问题


I have looked at the source code in jquery for slideup...

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

I understand that this is shorthand for functions, so I folllow through to GenFX

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

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

    return obj;
}

and then fxAttrs

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

but I just can't figure out how this code works, or how I would go about creating a slideLeft or slideRight that affects the width attribute of the HTML.


回答1:


You can use:

$('div').animate({ width: 'show' }); // slideLeft

$('div').animate({ width: 'hide' }); // slideRight

Demo at jsFiddle.




回答2:


You can do the same with slideRight as with slideUp:

$.fn.slideRight = function(options) {
    var s_opt = {
        speed: "slow",
        callback: null
    }

    $.extend(s_opt, options);
   return this.each(function() {
        $(this).effect("slide", null,
            s_opt.speed, s_opt.callback);
   });
};

Fiddle: http://jsfiddle.net/maniator/eVUsH/



来源:https://stackoverflow.com/questions/6154353/how-does-slideup-work-in-jquery-im-trying-to-make-my-own-slideright

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!