【问题标题】:Cannot extend div height when hidden divs within are made visible当隐藏的 div 可见时无法扩展 div 高度
【发布时间】:2013-10-09 06:09:47
【问题描述】:

我有一个<div>,其中包含另一个<div>,后者又包含一系列<div>s,可以切换。我已将前两个<div>s 设置为height: 100%;。所以现在当我有太多的<div>s 可见并且它不适合屏幕时,主要的<div> 不会扩展,让bg 成为body 的颜色。

这是(简化的)html 的样子:

<div id="wrapper">
    <div class="col" id="col1"></div>
    <div class="col" id="col2"></div>
    <div class="col" id="col3"></div>
</div>

还有相关的CSS

body, html {
    background-color: darkgray;
    margin: 0;
    height: 100%;
    width: 100%;
}

#wrapper {
    width: 80%;
    height: 100%;
    margin: 0 auto;
    margin-top: 0;      
    margin-bottom: 0;
    padding: 0;
    background-color: #666;
    color: white;
    min-width: 722px;
    max-width: 1119px;
    font-family: 'Roboto Condensed', sans-serif;
    box-shadow: 0px 0px 25px black;
}

.col {
    /*background-color: red;*/
    height: 100%;
    float:left;
    margin-top: 0;
    margin-right: 0.6667%;
    margin-left: 0.6667%;
}

#col1 {
    width: 24%;
}

#col2 {
    width: 48%;
    box-shadow: 0px 0px 5px black;
}

#col3 {
    width: 24%;
}

这是jsfiddle 中的样子。

这是问题的屏幕截图: 我尝试将以下代码添加到jQuery(在.click函数内)来解决问题:

var top = $('#col2:last-child').position().top;

var bottom = top + $('#col2:last-child').height();
$('#wrapper').css('height', bottom);

在 chrome 中,在控制台中出现以下错误:

Uncaught TypeError: Cannot read property 'top' of undefined 

如果可以的话,我真的很想在CSS 内解决这个问题。

【问题讨论】:

    标签: jquery css html height


    【解决方案1】:

    您可能希望将overflow:auto; 添加到该#wrapperJsFiddle

    要么这样,要么清除你的div's

    编辑:如果你想让 #wrapper 包含整个文本,而不是可滚动,你可以将它设置为 height:auto;JsFiddle

    【讨论】:

    • 好的,我不希望它可以滚动,但是height:auto; 使div 的深灰色消失了。我得到了较浅的灰色背景。
    • 您是否也将overflow:auto; 设置为#wrapper,还是仅设置height:auto;?检查我的答案的第二个小提琴,你应该看到一个工作场景。
    • 好的,确实如此。但是当我这样做时,我会失去两件事。我快要失去理智了,我无法让这三件事发挥作用。我还想给#wrapper 一个min-height:100%;,这样我的底部就没有浅灰色的背景了。另外,我希望有一个完整的高度#col2,这样当其中的divs 被jQuery 隐藏时,我仍然有一个延伸到页面底部的box-shadow,就像深灰色背景一样.
    • 我认为这不可能。除非您想让实际的 #wrapper 可滚动。为了做你想做的事,你必须为#wrapper 设置一个明确的高度。除非你想做类似#col2 { min-height: 600px; } 的事情。这会起作用,但不会非常“动态”
    • 因为你已经有了 jQuery,你可以使用类似$('#col2').css('min-height', window.innerHeight-30); 的东西来实现你想要的。这应该这样做。 jsfiddle.net/HbjtG/6
    【解决方案2】:

    我想我会使用 display: inline-block; 而不是 float: left; 个人偏好,但如果可能的话,我会尽量避免弄乱文档流程(这可能会导致您所看到的问题)。

    Working Example

    #wrapper {
        width: 80%;
        margin: 0 auto;
        padding: 10px;
        background-color: #666;
        color: white;
        min-width: 722px;
        max-width: 1119px;
        font-family:'Roboto Condensed', sans-serif;
        box-shadow: 0px 0px 25px black;
    }
    .col {
        width: 24%;
        /*background-color: red;*/
        display:inline-block;
        margin-top: 0;
        vertical-align:top;
    }
    #col2 {
        width: 48%;
        box-shadow: 0px 0px 5px black;
    }
    p {
        text-align: justify;
        margin-left: 5%;
        margin-right: 5%;
    }
    img {
        margin-top: 10px;
        display: inline-block;
        padding: 5%;
    }
    ul {
        text-align: justify;
        margin-left: -5%;
        margin-right: 5%;
    }
    #col2 h1 {
        line-height: 1em;
        text-align: left;
        padding: 2.5%;
        margin-top: 0;
        margin-bottom: 0;
        background-color: #444;
        border-bottom: solid 1px black;
        cursor: pointer;
        text-shadow:0px 0px 6px black;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    #col2 ul {
        list-style: none;
        margin-left:-2.5%;
    }
    .col em {
        color:#000;
        cursor: pointer;
    }
    .col em:hover {
        text-decoration: underline;
    }
    #homepageButton {
        width: 50px;
        height: 50px;
        left: 15px;
        bottom: 0;
        position: fixed;
        background-color: #444;
        margin-bottom: 0;
        box-shadow: 0px 0px 5px black;
        padding: 5px;
        text-align: center;
    }
    #homepageButton a {
        text-decoration: none;
        color: #fff;
        text-shadow:0px 0px 6px black;
    }
    #homepageButton a:hover {
        text-decoration: underline;
    }
    * {
        -moz-box-sizing:border-box;
        -webkit-box-sizing:border-box;
        box-sizing:border-box;
    }
    

    我还在所有内容中添加了box-sizing: border-box;...参见:Paul Irish's explanation for that

    【讨论】:

    • 我还添加了min-height: 100%;,这正是我想要的。谢谢。
    【解决方案3】:

    这就是你所追求的吗? http://jsfiddle.net/sodiumnitrate/HbjtG/1/

    我在你的包装器中添加了一个.clearfix,我将包装器强制为全高,以使其至少占据整个视口,但如果内容需要它会增长。

    /* clearfix */
    .clearfix:before,
    .clearfix:after {
      content: ".";
      display: block;
      height: 0;
      overflow: hidden;
    }
    .clearfix:after {
      clear: both;
    }
    .clearfix {
      zoom: 1; /* IE < 8 */
    }
    
    #wrapper {
      /* force full height */
      min-height: 100%;
      height: auto !important;
      height: 100%;    
      ...
    

    【讨论】:

    • 这不能正确修复它。第二个.researchContent 中的内容溢出div#wrapper。你需要做更多的清理工作。
    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 2012-05-10
    • 1970-01-01
    • 2022-01-21
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多