繁体   English   中英

jQuery - 设置div的最小高度

[英]jQuery - Set min-height of div

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

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

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

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

只是一个概念证明!

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

这应该按预期工作!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM