Javascript jQuery 动画宽度向右

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14297985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:18:39  来源:igfitidea点击:

jQuery animate width to the right

javascriptjqueryanimation

提问by bushdiver

I want to animate div to slide to the right like the example below:

我想为 div 设置动画以向右滑动,如下例所示:

http://jsfiddle.net/56hxy/3/

http://jsfiddle.net/56hxy/3/

but instead of shifting the margin, I want to "squish" the width but it's not working by changing the width % ( width: '60%') it squishes to the left instead..

但我不想移动边距,而是想“挤压”宽度,但它无法通过更改宽度 % ( width: '60%') 而向左挤压。

回答by Fabrício Matté

Not sure if this is exactly what you're looking for but try:

不确定这是否正是您要找的,但请尝试:

$('#content').animate({
    marginLeft: '40%',
    width:'60%'
});

Fiddle

小提琴



Or give right:0to #contentin the CSS, then you can animate the widthand it will shrink from left to right without requiring margins. Same effect as the above but cleaner. Fiddle

或者在 CSS 中给right:0to #content,然后您可以设置动画width,它会从左到右缩小而不需要边距。效果与上述相同,但更干净。小提琴

Also, jQuery has a built-in "toggle event" for alternating clicks so, if you're toggling just the click event you can use:

此外,jQuery 具有用于交替点击的内置“切换事件”,因此,如果您只切换点击事件,则可以使用:

$('#content').toggle(function() { 
    $(this).animate({ width:'60%' }); 
}, function() {
    $(this).animate({ width:'100%' }); 
});

Fiddle

小提琴