views:

37

answers:

2

I want to use the effect of slideDown() for the first div to show it, starting from the dynamically generated height of last div to a height dynamically generated by its content within?

In the code bellow the part with the problem is .animate({'height':'200px'} / .animate({'height':'100px'} , the value 200px and 100px should be dynamically generated.

    <!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"&gt;&lt;/script&gt;
 </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>
A: 

Try something like this:

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

Found the answer. I had no idea that you can get the dimensions of hidden elements, and the solution came naturally..

    <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>

Does not work with 'height':'auto'.

ps. I am AlqQ. I guess I registered some time ago and now when I log in with an OpenID it redirects me to this old account, which is fine by me.

Alqin