LoginSignup
4
3

More than 5 years have passed since last update.

jQueryで高さを固定長からautoアニメーションで変更させる方法

Posted at

使ったサイト。
なろう廃人のすすめ
ezgif.com-resize.gif

元のheightをcssで100pxに設定、
そこからアニメーションでheightをautoにする。

autoにした場合の具体的なheightの数値をjqueryで取得、
その数値に向かってanimateさせることで
autoに向かってアニメーションで変更することを実装。

      //現在のheight取得
      curHeight = $(this).height();
      //autoにした場合のheightを取得
      autoHeight = $(this).css('height', 'auto').height();
      //autoにした場合のheightへ向かってanimate
      //数値なのでanimateが有効
      $(this).height(curHeight).animate({height: autoHeight}, 150);

戻す場合は普通に数値指定すればOK。

      $(this).animate({height: "100px"}, 150);
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3