2010-05-01 181 views
23

对于大多数人来说,这可能非常简单。但我需要一个小片段来查看div的当前高度(div有一个基于其内容的动态高度) ,然后在css类的最小高度值中设置该值。jQuery - 设置div的最小高度

基本上这意味着我希望这个容器的最小高度与当前高度完全相同。这可能是一个快速:)

回答

6

只是一个概念的证明!

 // attend for dom ready 
    $(function() { 
     // hide .foo before we get it's height 
     $('.foo').hide(); 
     // get the height, also the one mentioned below is a good one! 
     var foo_height = $('.foo').height(); 
     // see if the actual height respect our needs! esample 180px 
     if (foo_height > 180) { 
      // set the height and show it! 
      //$('.foo').css('height', foo_height + 'px').show(); OR try 
      $('.foo').height(foo_height).show(); 
     } else { 
      //do something else here 
      } 
}); 

这应该按预期工作!

69

如果我理解正确的话,你可以做到以下几点:

$(".foo").css("min-height", function(){ 
    return $(this).height(); 
}); 
+0

是的,这是工作:) 的原因,我问的是,因为我已经有了之前它是由其他的内容填充,没有指定的高度一个div被清空。根据其中的内容制作整个容器的动态大小。在无内容和内容之间的这个小小差距中,div在缩小到它所需的大小之前缩小到它的最小高度。这里的缺点是div不会动态缩小,只能向上。你知道解决这个问题的好方法吗? – 2010-05-01 18:10:28

1
var elt = document.getElementById("<%= this.your_element_id.ClientID %>"); 
elt.style.minHeight = elt.clientHeight + 'px';