2010-06-24 133 views
0

我想使用第一个div的slideDown()效果来显示它,从动态生成的最后一个div的高度到由其内容动态生成的高度开始?如何将div的高度从动态值设置为动态高度值?

在下面的代码中,问题的部分是.animate({'height':'200px'}/.animate({'height':'100px'}),应该动态生成值200px和100px。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
<head> 
    <title>Slide Down</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <style type="text/css"> 
    div {background:#de9a44;width:80px; } 
    </style> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
</head> 
<body> 
    <div style="display: none"> 
    In pellentesque risus sit amet magna consectetur nec consequat eros ornare. 
    </div> 
    <div>Vivamus placerat eleifend rutrum</div> 
    Click! 
    <script type="text/javascript"> 
    $(document.body).click(function() { 
    if ($("div:first").is(":hidden")) { 
    var height = $("div:last").height(); 
    $("div:last").hide(); 
    $("div:first").height(height); 
    $("div:first").animate({'height':'200px'},"slow"); 
    } else { 
    var height = $("div:first").height(); 
    $("div:first").hide(); 
    $("div:last").height(height); 
    $("div:last").animate({'height':'100px'},"slow"); 
    } 
    }); 

    </script> 
</body> 
    </html> 
+0

从哪里到目标身高应该从哪里来? – 2010-06-24 17:34:11

回答

0

尝试是这样的:

$("div:last").animate({'height':'auto'},"slow"); 
0

找到了答案,我不知道,你可以得到隐藏要素的尺寸,使溶液自然就来了..

<script type="text/javascript"> 
     $(document.body).click(function() { 
      if ($("div:first").is(":hidden")) { 
       var hidden = $("div:first").height(); 
       var showing = $("div:last").height(); 
       $("div:last").hide(); 
       $("div:first").height(showing); 
       $("div:first").animate({'height':hidden},"slow"); 
      } else { 
       var hidden = $("div:last").height(); 
       var showing = $("div:first").height(); 
       $("div:first").hide(); 
       $("div:last").height(showing); 
       $("div:last").animate({'height':hidden},"slow"); 
      } 
     }); 
    </script> 

不适用'高度':'自动'。

ps。我是AlqQ。我想我前一段时间注册了,现在当我用OpenID登录时,它将我重定向到这个旧帐户,这对我来说很好。