jQuery - 设置div的最小高度

Ken*_*nes 23 javascript css jquery

这对你们大多数人来说可能都很容易.但是我需要一个小片段来查找div的当前高度(div具有基于其内部内容的动态高度),然后在css类的min-height值中设置该值.

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

Sam*_*son 69

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

$(".foo").css("min-height", function(){ 
    return $(this).height();
});
Run Code Online (Sandbox Code Playgroud)


Luc*_*ofi 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
            }
});
Run Code Online (Sandbox Code Playgroud)

这应该按预期工作!