jQuery animate height

前端 未结 3 1681
刺人心
刺人心 2020-12-01 14:13

I have a button and a div with inside some text:

Click me

Lot of text here....

3条回答
  •  臣服心动
    2020-12-01 15:05

    You can store the height just before slimming it down to 30px on page load, for example:

    $(".expand").each(function() {
      $.data(this, "realHeight", $(this).height());
    }).css({ overflow: "hidden", height: "30px" });
    

    Then in your click handler:

    $("h2").toggle(function() {
      var div = $(this).next(".expand")
      div.animate({ height: div.data("realHeight") }, 600);
    }, function() {
      $(this).next(".expand").animate({ height: 30 }, 600);
    });
    

    So what this does is get the .height() (run this in document.ready) before the overflow: hidden it set, and stores it via $.data(), then in the click handlers (via .toggle() to alternate), we use that value (or 30) every other click to .animate() to.

提交回复
热议问题