简体   繁体   English

如何根据内容设置aa div的高度?

[英]How to set height of a a div based on its content?

So I am working on this website and I have a tabbed section towards the bottom. 所以我正在这个网站上工作,我有一个标签部分朝下。 I built my tabs using css just like this . 我就像这样用css构建我的标签。 But a problem arose when i wanted the tab content to be different sizes. 但是当我希望标签内容的大小不同时,出现了一个问题。

I figured if I could target the height of the content I would be able to set the height of .tabs with Javascript. 我想如果我可以定位内容的高度,我将能够使用Javascript设置.tabs的高度。

This is the code I'm using: 这是我正在使用的代码:

        $('.tab label').click(function() {
            $('.tab.current').removeClass('current');
            $(this).parent('.tab').addClass('current');
            $('.tabs').height($('.tab.current .content').outerHeight());
        });

For some reason its not grabbing the right value. 由于某种原因,它没有抓住正确的价值。 I dont know what Im doing wrong. 我不知道我做错了什么。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Edit: The tab structure looks like this: 编辑:选项卡结构如下所示:

           <div class="tabs">

           <div class="tab one current">
                <input type="radio" id="tab-1" name="tab-group-1" checked>
                <label for="tab-1"></label>

                <div class="content">
                    <div class="tab-block first">
                </div><!-- .content -->
           </div><!-- .tab.one -->

           <div class="tab two">
                <input type="radio" id="tab-1" name="tab-group-1" checked>
                <label for="tab-1"></label>

                <div class="content">
                    <div class="tab-block first">
                </div><!-- .content -->
           </div><!-- .tab.two -->

           <div class="tab three">
                <input type="radio" id="tab-1" name="tab-group-1" checked>
                <label for="tab-1"></label>

                <div class="content">
                    <div class="tab-block first">
                </div><!-- .content -->
           </div><!-- .tab.three -->

           </div>

The .tabs class Requires a min-height due to the fact that the tabs are being positioned absolutely on top of one another. .tabs类需要一个最小高度,因为标签绝对位于彼此之上。

So what I would like to do is grab the height of the .content of the .current classand set that height to the .tabs div. 所以我想要的是获取.current类的.content的高度并将该高度设置为.tabs div。 so that instead of the height overflowing it will push all content underneath it. 因此,不是高度溢出,它将推动它下面的所有内容。

I hope this clears up some of the confusion. 我希望这能解决一些困惑。

There are several things causing this issue. 有几件事导致了这个问题。 The main one being CSS3 transition. 主要是CSS3过渡。 The div is animating height when the .current class is being applied. 当应用.current类时,div是动画高度。 The javascript is grabbing the value before the CSS3 animation completes which is why you are not getting the correct value. javascript是在CSS3动画完成之前获取值,这就是为什么你没有得到正确的值。

Instead of using javascript, a cleaner solution would be to apply classes to div.tabs: 而不是使用javascript,更清洁的解决方案是将类应用于div.tabs:

div.tabs.currentOne {
     height: 250px;
}

div.tabs.currentTwo {
     height: 1022px;
}

div.tabs.currentThree {
     height: 882px;
}

And in your javascript: 并在您的JavaScript中:

$('.tab label').click(function () {
    var $tabs = $('.tabs');
    $tabs.find('.tab.current').removeClass('current');
    var $currTab = $(this).parent('.tab').addClass('current');

    // remove any one of the classes that we'll apply
    $tabs.removeClass('currentOne currentTwo currentThree');

    // determine which one to apply
    if ($currTab.hasClass('one')) {
        $tabs.addClass('currentOne');
    }
    else if ($currTab.hasClass('two')) {
        $tabs.addClass('currentTwo');
    }
    else if ($currTab.hasClass('three')) {
        $tabs.addClass('currentThree')
    }
});

Note that the height of div.tabs has to consider padding and borders of child elements, and height of the labels. 请注意,div.tabs的高度必须考虑子元素的填充和边框以及标签的高度。 It is the height of div.content class + the padding + the 2px bottom padding + 40px for labels. 它是div.content类的高度+填充+ 2px底部填充+ 40px的标签。

Also be sure to add the correct class to the tab that will default to visible. 还要确保将正确的类添加到默认为可见的选项卡。

In your target class set the height:auto 在目标类中设置高度:auto

.your_class{
height:auto;
}

Add this class to the element you want to assign. 将此类添加到要分配的元素。

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

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