How does slideUp() work in jQuery? I'm trying to make my own slideRIght()
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" ],
开发者_Python百科 // 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.
You can use:
$('div').animate({ width: 'show' }); // slideLeft
$('div').animate({ width: 'hide' }); // slideRight
Demo at jsFiddle.
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/
精彩评论