Google Chrome 使用 Flexbox 视口锚定扩展方向
P粉852578075
2023-08-25 17:44:46
<p>Google Chrome 中存在一个问题,当元素放置在具有<code>之间有空格</code的<em>相邻</em> Flex 项目的 Flexbox 容器内时,元素会相对于视口以不同方向展开/折叠> 或 <code>center</code> 对齐内容。</p>
<p>这在 Firefox、IE11、Edge 或 Safari 中不是问题,因为元素始终向下扩展。</p>
<p>我很好奇:</p>
<ul>
<li>Chrome 的行为是否遵循某些规范?如果有,是哪一个?</li>
<li>如果不是,那为什么要在 Chrome 中这样做? (恕我直言,点击触发器在屏幕外随机消失是可怕的用户体验。)</li>
<li>可以通过某种方式修改或禁用 Chrome 的行为吗?例如。通过 CSS 或元标记?</li>
</ul>
<p><strong>更新 1:如果可能的话,我已在 chromium bug tracker 上提交了问题 #739860,寻求 Chromium 开发人员的见解/解释。</strong></p>
<hr>
<p>下面插入了两个示例。描述该问题的完整测试套件可以在这支笔中找到:https://codepen.io/jameswilson/full/xrpRPg/ 我选择在本示例中使用 slipToggle,以便展开/折叠运动是动画的并且对眼。详细信息标签也会发生相同的行为,但还没有跨浏览器支持,并且展开/折叠太卡顿了。</p>
<p><strong>示例 1:Chrome 针对对齐 Flexbox 之间的空格的展开/折叠行为</strong></p>
<p>
<pre class="brush:js;toolbar:false;">$('button').click(function() {
$(this).next().slideToggle();
})</pre>
<pre class="brush:css;toolbar:false;">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;
}</pre>
<pre class="brush:html;toolbar:false;"><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></pre>
</p>
<p><strong>示例 2:Chrome 对居中对齐的 Flexbox 的展开/折叠行为</strong></p>
<p>
<pre class="brush:js;toolbar:false;">$('button').click(function() {
$(this).next().slideToggle();
})</pre>
<pre class="brush:css;toolbar:false;">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;
}</pre>
<pre class="brush:html;toolbar:false;"><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></pre>
</p>
将此代码添加到您的 Flex 容器中:
溢出锚:无
这将禁用 Chrome 中称为“滚动锚定”的功能,该功能会导致框向上扩展 (修订的代码笔)。
在 Chrome 中,展开框的向上/向下方向由视口中的滚动位置决定,而不是布局本身。
为了改善用户体验,Chrome 针对这种行为采取了独特的方法。
基本上,它们将 DOM 元素绑定到当前滚动位置。屏幕上这个特定(“锚点”)元素的移动将确定对滚动位置的调整(如果有)。
他们将这种方法称为“滚动锚定”。该算法在此页面上进行了解释: https://github.com/WICG/ScrollAnchoring/blob/master/explainer.md
此行为目前是 Chrome 独有的,但Google 正在推动标准化。
根据滚动锚定文档,将
overflow-anchor: none
应用到适当的元素将禁用滚动锚定调整。更多信息: