在onClick上切换DIV高度

Dio*_*ion 1 html css jquery

我想切换一个分区的高度。我尝试将.animate与if / else语句一起使用。但它只会反弹。我现在使用的代码将隐藏我的部门而不是切换高度。单击时将触发。

$(document).ready(function() {
       $("#content1").toggle(function(){
         $(this).animate({height:'400px'});
   }, function() {
         $(this).animate({height:'200px'});
   });
});
Run Code Online (Sandbox Code Playgroud)

我希望有人能找到答案,因为我无法在google / stackoverflow上找到答案。

BaS*_*Gaz 5

为什么不.animate()直接使用它来设置div的高度并在每次单击div时为更改设置动画!

喜欢:

$( "div" ).click(function() {
    if ( $(this).height() != 50)
          $( this ).animate({ height: 50 }, 1000 );
    else
          $( this ).animate({ height: 100 }, 1000 );
});
Run Code Online (Sandbox Code Playgroud)

这是它的完整示例:http : //jsfiddle.net/62jcH/3/