【问题标题】:jQuery - Set min-height of divjQuery - 设置 div 的最小高度
【发布时间】:2015-10-07 23:53:04
【问题描述】:

这对你们大多数人来说可能真的很容易。但我需要一个小的 sn-p 来查找 div 的当前高度(div 具有基于其中内容的动态高度) 然后将该值设置为 css 类的 min-height 值。

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

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    如果我理解正确,您可以执行以下操作:

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

    【讨论】:

    • 是的,这是有效的 :) 我问的原因是因为我有一个没有指定高度的 div 在它被其他内容填充之前被清空。根据里面的内容使整个容器的大小动态变化。在没有内容和内容之间的这个小间隙中,div 在增长到所需大小之前会缩小到最小高度。这里的缺点是 div 的大小不会动态减小,只会向上。你知道解决这个问题的好方法吗?
    【解决方案2】:

    只是概念验证!

         // 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
                }
    });
    

    这应该可以按预期工作!

    【讨论】:

      【解决方案3】:
      var elt = document.getElementById("<%= this.your_element_id.ClientID %>");
      elt.style.minHeight = elt.clientHeight + 'px';
      

      【讨论】:

        猜你喜欢
        • 2014-09-17
        • 1970-01-01
        • 2011-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-27
        • 2012-04-05
        • 1970-01-01
        相关资源
        最近更新 更多