how do i animate a specific height to 100% in jquery

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:02:35

Although I am sure there is a better way of doing this as this method is sloppy AT BEST, if you don't find another solution you could always try this:

$('.show_it').click(function() {

// Animate to 100% in 1 millisecond
$(this).parent().next("div").animate({height: 'auto'}, 1);

// Record the height of the div
var newHeight = $(this).parent().next("div").height();

// Animate height to 0% in 1 millisecond
$(this).parent().next("div").animate({height: '0px'}, 1);

// Do the animation using the new fixed height.
  $(this).parent().next("div").animate({
    height: newHeight,
  }, 1000, function() {
  });
});

However, as I said this is very sloppy - if it fits your needs, try jquery slideDown(); this is a much better way to do it.

Or this:

$('.show_it').click(function(){
   $(this).parent().next("div").css("height", "100%");
   var h = $(this).parent().next("div").height();
   $(this).parent().next("div").css("height", "0px");
   $(this).parent().next("div").animate({height: h}, 1000, function() {
   });
});

Try this

$('.show_it').click(function() {
  var $div = $(this).parent().next("div");
  var h = $div.children().each(function(){ h = h+ $(this).height(); });
  $div.animate({
    height: h
  }, 1000, function() {
  });
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!