无法滚动到溢出容器的flex项目的顶部的标题重写为:无法将溢出容器中的flex项目滚动至顶部
<p>在尝试使用flexbox创建一个有用的模态框时,我发现了一个似乎是浏览器问题的情况,并想知道是否有已知的修复方法或解决方法 - 或者对如何解决它有什么想法。</p>
<p>我试图解决的问题有两个方面。首先,垂直居中模态窗口,这个功能按预期工作。第二个是让模态窗口滚动 - 外部滚动,所以整个模态窗口滚动,而不是其中的内容(这样你可以有下拉菜单和其他可以超出模态框范围的UI元素 - 如自定义日期选择器等)。</p>
<p>然而,当将垂直居中与滚动条结合使用时,模态框的顶部可能变得无法访问,因为它开始溢出。在上面的示例中,您可以调整大小以强制溢出,在这样做时,它允许您滚动到模态框的底部,但无法滚动到顶部(第一段被截断)。</p>
<p><br /></p>
<pre class="brush:css;toolbar:false;">.modal-container {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0, 0, 0, 0.5);
overflow-x: auto;
}
.modal-container .modal-window {
display: -ms-flexbox;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* Optional support to confirm scroll behavior makes sense in IE10
//-ms-flex-direction: column;
//-ms-flex-align: center;
//-ms-flex-pack: center; */
height: 100%;
}
.modal-container .modal-window .modal-content {
border: 1px solid #ccc;
border-radius: 4px;
background: #fff;
width: 100%;
max-width: 500px;
padding: 10px
}</pre>
<pre class="brush:html;toolbar:false;"><div class="modal-container">
<div class="modal-window">
<div class="modal-content">
<p class="p3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p class="p3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p class="p3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</div>
</div></pre>
<p><br /></p>
<p>这影响(当前的)Firefox,Safari,Chrome和Opera。有趣的是,在IE10中,如果您取消注释IE10供应商前缀的CSS,它的行为确实是正确的 - 我还没有在IE11中进行测试,但假设行为与IE10相匹配。</p>
<p>这是示例代码的链接(高度简化)</p>
<p>https://jsfiddle.net/dh9k18k0/2/</p>
我只用了3个容器就实现了这个效果。诀窍是将flexbox容器与控制滚动的容器分开。最后,将所有内容放入一个根容器以居中显示。以下是创建效果所需的关键样式:
我在这里创建了一个演示:https://jsfiddle.net/r5jxtgba/14/
问题
Flexbox使居中变得非常容易。
只需将
align-items: center
和justify-content: center
应用于flex容器,您的flex项目将垂直和水平居中。然而,当flex项目大于flex容器时,这种方法存在一个问题。
如问题中所指出的,当flex项目溢出容器时,顶部变得无法访问。
对于水平溢出,左侧部分变得无法访问(或者在RTL语言中为右侧部分)。
下面是一个LTR容器的示例,其中包含
justify-content: center
和三个flex项目:有关此行为的说明,请参见本答案底部。
解决方案#1
为了解决这个问题,请使用flexbox自动边距,而不是
justify-content
。使用
auto
边距,溢出的flex项目可以在垂直和水平方向上居中,而不会失去对其任何部分的访问。所以,不要在flex容器上使用这段代码:
在flex项目上使用这段代码:
修订演示
解决方案#2(大多数浏览器尚未实现)
将
safe
值添加到您的关键字对齐规则中,如下所示:或者
来自CSS Box Alignment模块规范:
注意:Box Alignment模块适用于多个盒子布局模型,而不仅仅是flex。因此,在上述规范摘录中,方括号中的术语实际上是“对齐主体”、“对齐容器”和“
start
”。我使用了特定于flex的术语,以保持对这个特定问题的关注。来自MDN的滚动限制说明: