如何获得嵌套的div容器高度

时间:2016-02-15 19:35:13

标签: javascript html css

我有两个嵌套的垂直布局容器,每个容器可以有一个顶部和底部比例内容,其中底部也有一个嵌套分割器。如何获得中间高度(数字3)。 BoundingClientRect给出与浏览器检查相同的高度,而不是可见高度。见附图。这里还包括顶部相邻分流器高度。它也可能是一个问题,如何通过浏览器检查来改变这种布局以获得正确的高度?

    <style>
    html, body {
        height: 100%;
    }
    .container {        
        background: rgba(221, 221, 221, 0.41);
        width: 100%;
        height: 100%;
    }
    .verticalContainer {
        width: 100%;
        height: 100%;
        margin: auto;
        overflow: hidden;
    }
    .topContent {
        width: 100%;
        margin: auto;
        overflow: hidden;
    }
    .bottomContent {
        width: 100%;      
        margin: auto;
        overflow: hidden;
    }
    .splitter  {
        background: rgba(31, 37, 37, 0.41);
        margin-top: 10px;
        margin-bottom: 10px;
        height: 10px;
        width: 100%;
        cursor: n-resize;
    }                   
    </style>
    <div class="verticalContainer">    
        <div class="topContent" style="height:65%;">

            <!--nested container-->
            <div class="verticalContainer">

                <div class="topContent" style="height:65%;">        
                    <div class="container">2</div>        
                </div>

                <div class="bottomContent" style="height:35%;">        
                    <div class="splitter"></div>
                    <div class="container">3</div>        
                </div>

            </div>
            <!------------>

        </div>

        <div class="bottomContent" style="height:35%;">        
            <div class="splitter"></div>
            <div class="container">1</div>        
        </div>            
    </div>

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个。它可能会帮助您获得3号集装箱的高度

$(document).ready( function() {
  alert($(".verticalContainer .bottomContent .container").height());
});
html, body {
  height: 100%;
}
.container {        
  background: rgba(221, 221, 221, 0.41);
  width: 100%;
  height: 100%;
}
.verticalContainer {
  width: 100%;
  height: 100%;
  margin: auto;
  overflow: hidden;
}
.topContent {
  width: 100%;
  margin: auto;
  overflow: hidden;
}
.bottomContent {
  width: 100%;      
  margin: auto;
  overflow: hidden;
}
.splitter  {
  background: rgba(31, 37, 37, 0.41);
  margin-top: 10px;
  margin-bottom: 10px;
  height: 10px;
  width: 100%;
  cursor: n-resize;
} 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="verticalContainer">    
  <div class="topContent" style="height:65%;">

    <!--nested container-->
    <div class="verticalContainer">

      <div class="topContent" style="height:65%;">        
        <div class="container">2</div>        
      </div>

      <div class="bottomContent" style="height:35%;">        
        <div class="splitter"></div>
        <div class="container" id="third">3</div>        
      </div>

    </div>
    <!------------>

  </div>

  <div class="bottomContent" style="height:35%;">        
    <div class="splitter"></div>
    <div class="container">1</div>        
  </div>            
</div>