2016-02-04 71 views
0

我想將一些DOM放在一起,而且我真的不確定如何解決以下問題。我基本上需要有一個可見的.filterArea,並且它上面的區域佔據了剩餘的空間。難度是我想我的.filterArea根據寬度做出反應,而我不知道我的總高度是多少。根據剩餘空間自動調整分區大小(考慮以下分區)

body { 
 
    background-color: #fff; 
 
} 
 

 
.mainArea { 
 
    background: red; 
 
    color: white; 
 
} 
 

 
.filter-area .tags { 
 
    padding: 10px; 
 
} 
 

 
.tag { 
 
    margin: 5px; 
 
    padding: 5px; 
 
    background-color: #f6f6f6; 
 
    display: inline-block; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 

 
    <div class="mainArea"> 
 
    <p> 
 
    This is the main area 
 
    </p> 
 
    </div> 
 

 
    <div class="filterArea" data-bind="component: filterComponent"> 
 
    <div class="filter-area"> 
 
     <!-- Represent the different tags that can be applied as filters --> 
 
     <div class="tags"> 
 
     <div class="tag" data-tag="lessThan" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Less than</span> 
 
      <span class="value" data-bind="dl-siformat: arg1">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="lessThanOrEqualTo" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Less than or equal</span> 
 
      <span class="value" data-bind="dl-siformat: arg1">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="equals" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Equals</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="greaterThan" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Greater than</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="greaterThanOrEqualTo" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Greater than or equal</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="between" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Between</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
      <span class="name">and</span> 
 
      <span class="value" data-bind="dl-siformat: arg1">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="within" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Within</span> 
 
     </div> 
 
     <div class="tag" data-tag="empty" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Empty</span> 
 
     </div> 
 
     </div> 
 
     <div class="filters scrollable numberFilters filtersEmpty" data-bind="css: filters().length === 0 ? 'filtersEmpty' : ''"> 
 
     <div data-bind="dl-filter-scroll-top:$parent"></div> 
 
     <div data-bind="template: { foreach:filters, 
 
            name: function(item) { return item.func(); }, 
 
            afterAdd: showFilter, 
 
            beforeRemove: hideFilter }"></div> 
 
     </div> 
 
    </div> 
 
    </div>

我已經看到了一些解決方案,但圍繞它們都依賴於其知道.filterArea的固定高度。是否有一些CSS魔術可以在.filterArea被處理後根據剩餘空間調整頂部的大小?

+0

你能否闡述一下 「佔用剩餘的空間」?你的意思是.mainArea和.filter-area應該總計視口高度?要麼? – pivemi

+0

@pivemi - 是的。組合的高度應該佔據完整的視口,但是我希望'.filter-area'優先,並確保在'mainArea'計算它的高度之前它是可見的。 – Ian

+0

@pivemi我也有興趣知道,如果在頂部div(視口)添加「overflow-y:scroll」會導致任何問題?在這種情況下不需要,但可能需要滾動其他內容。 – Ian

回答

1

您可以使用CSS柔性盒以及使用vh單位的容器將總高度設置爲視口高度。

body { 
 
    background-color: #fff; 
 
} 
 

 
.container { 
 
    height: 100vh; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.mainArea { 
 
    background: red; 
 
    color: white; 
 
    flex: 1; 
 
    
 
} 
 

 
.filter-area .tags { 
 
    padding: 10px; 
 
} 
 

 
.tag { 
 
    margin: 5px; 
 
    padding: 5px; 
 
    background-color: #f6f6f6; 
 
    display: inline-block; 
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css" rel="stylesheet"/> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 

 
    <div class="mainArea"> 
 
    <p> 
 
    This is the main area 
 
    </p> 
 
    </div> 
 

 
    <div class="filterArea" data-bind="component: filterComponent"> 
 
    <div class="filter-area"> 
 
     <!-- Represent the different tags that can be applied as filters --> 
 
     <div class="tags"> 
 
     <div class="tag" data-tag="lessThan" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Less than</span> 
 
      <span class="value" data-bind="dl-siformat: arg1">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="lessThanOrEqualTo" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Less than or equal</span> 
 
      <span class="value" data-bind="dl-siformat: arg1">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="equals" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Equals</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="greaterThan" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Greater than</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="greaterThanOrEqualTo" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Greater than or equal</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="between" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Between</span> 
 
      <span class="value" data-bind="dl-siformat: arg0">0</span> 
 
      <span class="name">and</span> 
 
      <span class="value" data-bind="dl-siformat: arg1">0</span> 
 
     </div> 
 
     <div class="tag" data-tag="within" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Within</span> 
 
     </div> 
 
     <div class="tag" data-tag="empty" data-bind="click: addTag, dl-filterTagRipple"> 
 
      <span class="name">Empty</span> 
 
     </div> 
 
     </div> 
 
     <div class="filters scrollable numberFilters filtersEmpty" data-bind="css: filters().length === 0 ? 'filtersEmpty' : ''"> 
 
     <div data-bind="dl-filter-scroll-top:$parent"></div> 
 
     <div data-bind="template: { foreach:filters, 
 
            name: function(item) { return item.func(); }, 
 
            afterAdd: showFilter, 
 
            beforeRemove: hideFilter }"></div> 
 
     </div> 
 
    </div> 
 
    </div>

+1

你知道你可以添加堆棧溢出片段來解答嗎?而不必鏈接到JSFiddle呢? – Ian

+0

我沒有到現在。幾天前才加入。謝謝:) – pivemi

+0

我已經用一個代碼片段更新了答案 - 實際上適用於我的個人問題。感謝您的幫助 - stackoverflow points for you! :) – Ian

相關問題