【问题标题】:how to correctly make flex-box children to scroll如何正确使 flex-box 子项滚动
【发布时间】:2019-01-20 08:39:56
【问题描述】:

面对一些嵌套的flex-boxes,我卡住了,无法正确滚动内容。这是我的CSS:

html,body,#root{
   width:100%;
   height:100%;
   padding:0;
   margin:0;
}
.flex-fill{
   height:100%;
   width:100%;
   display:flex;
   align-items:stretch;
}
.flex-fill>div:last-child{
   flex-grow:1;
}
.flex-col{
   flex-direction:column;
}
<div id='root'>
  <div class='flex-fill flex-col'><!--actually scrolling div-->
     <div style="flex:0 0 35px"><h1>main title</h1></div>
     <div>
        <div class='flex-fill flex-col'>
           <div style="flex:0 0 30px"><h2>subtitle</h2></div>
           <div class='flex-fill'>
              <div style="flex:0 0 30%">...left side nav list...</div>
              <div class='flex-fill flex-col'>
                 <div style="flex:0 0 25px">...data header...</div>
                 <!--I hope the content of this div is scroll-able-->
                 <div style="overflow-y:scroll">...data list...</div>
              </div>
           </div>
        </div>
     </div>
  </div>
</div>

最里面的 div 中包含的数据列表很长,我想让它可以滚动。但事实上,上面的代码使根 div 的全部内容滚动。我对 flex 或 overflow 有误解吗?如何修复其他部分并滚动数据列表?

【问题讨论】:

  • /*这些应该是 cmets*/?如果是这样,HTML 会像这样 完成。无论如何,从我在这里看到的情况来看,它不会滚动,因为它可以放大到显示内容所需的大小。例如,将max-height: 50px;添加到要滚动的div中,如果内容高于50px,则需要滚动。
  • 对不起,我犯了错误。我在这里手动输入代码,将 /**/ 作为理所当然的注释。实际代码中没有这样的东西。真正的代码是用 React 编写的,而且很长。我将在浏览器中再次检查最终代码并修改我的问题。
  • 我不能将高度,即 50px 应用到数据列表容器 div,我希望它占据整个剩余空间。

标签: css scroll flexbox overflow


【解决方案1】:

已更新答案以保持您的 HTML 结构不变。内联样式被移除并作为类添加:

html,body{
   height:100%;
   padding:0;
   margin:0;
}
#root {
  height: 100%;
	display: flex;
}
.flex-fill{
   height:100%;
   display:flex;
   align-items:stretch;
}
.flex-col{
   flex-direction:column;
   display: flex;
   flex: 1 0 0;
   height: 100%;
}
.flex-row{
   flex-direction:row;
   display: flex;
   height: 100%;
}
.scrolling-div {
	height: 100%;
	overflow-y: scroll;
}
<div id='root'>
  <div class='flex-fill flex-col'>
     <div><h1>main title</h1></div>
     <div class="flex-col">
        <div class='flex-col'>
           <div><h2>subtitle</h2></div>
           <div class="flex-row">
              <div>...left side nav list...</div>
              <div class='flex-col'>
                 <div>...data header...</div>
                 <!--Now Scrolls-->
                 <div class="scrolling-div">
				 ...scrolling data list...<br />
				 ...scrolling data list...<br />
				 ...scrolling data list...<br />
				 ...scrolling data list...<br />
				 </div>
              </div>
           </div>
        </div>
     </div>
  </div>
</div>

【讨论】:

  • 你的代码改变了我的层次结构。我正在编写一个 React 应用程序,并且必须放弃一些 DOM 结构的简单性和较低的嵌套深度来保持我的组件系统的逻辑性。我的理想目标是找到一个答案,告诉我如何在任何嵌套深度正确组合弹性框和滚动能力。我找到了更深入的讨论 here ,但它仍然没有解决我的问题。现在我使用 css calc(100%- n) 函数来填充剩余空间而不是 flex-box,问题就解决了。
  • @WelsonZhong 我已经更新了我的答案以保持结构不变。
  • 你的方法有效。什么是魔法?用 height:100% 替换 flex-grow 是否完成工作?似乎 flex-grow 只是将内容拉伸到填充空间,但没有给出溢出需要工作的内容大小信息,因此溢出不能基于 flex-grow 工作。这只是我遇到这个问题后的猜测。如果您有一些文档解释您的答案或我的怀疑,我很想学习它。
  • 将可滚动的子级高度设置为 100% 使滚动工作。但它也引入了一个新问题。由于孩子与父母的身高相同,因此它的父母会比祖父占用的空间更多。 父高度=标题高度+父高度 最后一个“父高度”来自将可滚动的子高度设置为100%。这使父亲开始滚动。如果通过设置overflow=hidden来禁止父级滚动,那么即使滚动到底部,可滚动子级的最后一部分(高度=父级高度-标题高度)也将不可见。
【解决方案2】:

@Welson Zhong,请看下面的工作 sn-p,请在Full page 视图中查看,希望对您有所帮助:)

注意:太多的 flex 会杀死 flex ;) 那是你的代码的问题。

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
body {
  display: flex;
  flex-direction: column;
  font-family: sans-serif;
  font-size: 18px;
}
.main {
  display: flex;
  flex-direction: row;
  flex: 1 0 auto;
}
.sidebar {
  flex: 1 auto;
}
.content {
  display: flex;
  flex-flow: column nowrap;
  flex: 3 0px;
}
h1,h2,.data-header {
  flex: none;
}

/* below css is for visual purpose only */
.main *:not(.content) {
  box-shadow: inset 0 0 0 1px #ccc;
  padding: 10px;
}
<h1>main title</h1>
<h2>sub title</h2>
<div class="main">
  <div class="sidebar">
    ...left side nav list...
  </div>
  <div class="content">
    <div class="data-header">...data header...</div>
    <div style="overflow-y:scroll; flex: auto">...data list...</div>
  </div>
</div>

【讨论】:

  • 很抱歉,这是发布问题时的拼写错误。它实际上是 flex-direction。我使用 IDE 编写代码,这样的错别字会自动显示出来。最好找出我在渲染结构上的问题。
  • 请看上面更新的sn-p,现在它解决了你在渲染结构上的问题,请在Full page视图中查看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
相关资源
最近更新 更多