Google Chrome 中存在一个问题,当元素放置在具有之间有空格相邻 Flex 项目的 Flexbox 容器内时,元素会相对于视口以不同方向展开/折叠> 或 center 对齐内容。
这在 Firefox、IE11、Edge 或 Safari 中不是问题,因为元素始终向下扩展。
我很好奇:
更新 1:如果可能的话,我已在 chromium bug tracker 上提交了问题 #739860,寻求 Chromium 开发人员的见解/解释。
下面插入了两个示例。描述该问题的完整测试套件可以在这支笔中找到:https://codepen.io/jameswilson/full/xrpRPg/ 我选择在本示例中使用 slipToggle,以便展开/折叠运动是动画的并且对眼。详细信息标签也会发生相同的行为,但还没有跨浏览器支持,并且展开/折叠太卡顿了。
示例 1:Chrome 针对对齐 Flexbox 之间的空格的展开/折叠行为
$('button').click(function() {
$(this).next().slideToggle();
})
body {
margin: 30px;
font-family: sans-serif;
}
aside,
aside div,
summary,
main,
button,
details p,
button + p {
background: rgba(0,0,0,.09);
border: none;
padding: 20px;
margin: 0;
}
.flexcontainer {
display: flex;
}
aside {
flex: 1;
position: relative;
max-width: 25%;
background: mintcream;
display: flex;
flex-direction: column;
position: relative;
}
aside.space-between {
justify-content: space-between;
}
aside.center {
justify-content: center;
}
main {
flex: 3;
position: relative;
max-width: 75%;
background: aliceblue;
vertical-align: top;
height: 100%;
}
main > * + * {
margin-top: 20px;
}
button + p {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="flexcontainer">
<aside class="space-between">
<div>Top Sidebar</div>
<div>Bottom Sidebar</div>
</aside>
<main>
<div>
<button>slideToggle</button>
<p>Other browsers: always expands downward.<br>
Chrome: Should always expand downward because Top Sidebar is always visible.</p>
</div>
<div>
<button>slideToggle (usually expands down)</button>
<p>Other browsers: always expands downward.<br>
Chrome: Should expand downward while Bottom Sidebar and Top Sidebar are both visible. But will expand upward if you scroll down far enough so that Top Sidebar is off-screen.</p>
</div>
<div>
<button>slideToggle (usually expands down)</button>
<p>Other browsers: always expands downward.<br>
Chrome: Should expand downward while Bottom Sidebar and Top Sidebar are both visible. But will expand upward if you scroll down far enough so that Top Sidebar is off-screen.</p>
</div>
</main>
</section>
示例 2:Chrome 对居中对齐的 Flexbox 的展开/折叠行为
$('button').click(function() {
$(this).next().slideToggle();
})
body {
margin: 30px;
font-family: sans-serif;
}
aside,
aside div,
summary,
main,
button,
details p,
button + p {
background: rgba(0,0,0,.09);
border: none;
padding: 20px;
margin: 0;
}
.flexcontainer {
display: flex;
}
aside {
flex: 1;
position: relative;
max-width: 25%;
background: mintcream;
display: flex;
flex-direction: column;
position: relative;
}
aside.space-between {
justify-content: space-between;
}
aside.center {
justify-content: center;
}
main {
flex: 3;
position: relative;
max-width: 75%;
background: aliceblue;
vertical-align: top;
height: 100%;
}
main > * + * {
margin-top: 20px;
}
button + p {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="flexcontainer">
<aside class="center">
<div>Center Sidebar</div>
</aside>
<main>
<div>
<button>slideToggle (usually expands downwards)</button>
<p>Other browsers: always expands downward.<br>
Chrome: Usually expands downwards. Expands in both directions when the top-edge of the container scrolls out of the viewport.</p>
</div>
<div>
<button>slideToggle</button>
<p>Other browsers: always expands downward.<br>
Chrome: Usually expands downwards. Expands in both directions when the top-edge of the container scrolls out of the viewport.</p>
</div>
<div>
<button>slideToggle (usually expands downwards)</button>
<p>Other browsers: always expands downward.<br>
Chrome: Usually expands downwards. Expands in both directions when the top-edge of the container scrolls out of the viewport, but then resumes expanding downwards again when Center Sidebar scrolls out of view.</p>
</div>
</main>
</section>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
将此代码添加到您的 Flex 容器中:
溢出锚:无这将禁用 Chrome 中称为“滚动锚定”的功能,该功能会导致框向上扩展 (修订的代码笔)。
在 Chrome 中,展开框的向上/向下方向由视口中的滚动位置决定,而不是布局本身。
为了改善用户体验,Chrome 针对这种行为采取了独特的方法。
基本上,它们将 DOM 元素绑定到当前滚动位置。屏幕上这个特定(“锚点”)元素的移动将确定对滚动位置的调整(如果有)。
他们将这种方法称为“滚动锚定”。该算法在此页面上进行了解释: https://github.com/WICG/ScrollAnchoring/blob/master/explainer.md
此行为目前是 Chrome 独有的,但Google 正在推动标准化。
根据滚动锚定文档,将
overflow-anchor: none应用到适当的元素将禁用滚动锚定调整。更多信息: