Google Chrome 使用 Flexbox 视口锚定扩展方向

2024-03-11

Google Chrome 中存在一个问题,当元素放置在带有adjacent弹性项目有space-between or center合理的内容。

这在 Firefox、IE11、Edge 或 Safari 中不是问题,因为元素始终向下扩展。

我很好奇:

  • Chrome 的行为是否遵循某些规范?如果有,是哪一个?
  • 如果不是,那为什么要在 Chrome 中这样做呢? (恕我直言,点击触发器随机消失在屏幕外是可怕的用户体验。)
  • Chrome 的行为可以以某种方式修改或禁用吗?例如。通过 CSS 还是元标记?

更新1:我已经提交了chromium bug tracker 上的问题#739860 https://bugs.chromium.org/p/chromium/issues/detail?id=739860如果可能的话,向 Chromium 开发人员寻求见解/解释。


下面是插入的两个示例。描述问题的完整测试套件可以在这支笔中找到:https://codepen.io/jameswilson/full/xrpRPg/ https://codepen.io/jameswilson/full/xrpRPg/我在本例中选择使用 SlideToggle,以便展开/折叠动作具有动画效果并且肉眼可见。详细信息标签也会发生相同的行为,但尚不支持跨浏览器,并且展开/折叠太卡顿了。

例 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>

将此代码添加到您的 Flex 容器中:

  • overflow-anchor: none

这将禁用 Chrome 中称为“滚动锚定”的功能,该功能会导致框向上扩展(修改后的代码笔 https://codepen.io/anon/pen/NgLBpv).


在 Chrome 中,展开框的向上/向下方向由视口中的滚动位置决定,而不是布局本身。

Chrome 针对这种行为采取了独特的方法,以改善用户体验。

基本上,它们将 DOM 元素绑定到当前滚动位置。屏幕上这个特定(“锚点”)元素的移动将确定对滚动位置的调整(如果有)。

他们将这种方法称为“滚动锚定”。该算法在此页面上进行了解释:https://github.com/WICG/ScrollAnchoring/blob/master/explainer.md https://github.com/WICG/ScrollAnchoring/blob/master/explainer.md

此行为目前是 Chrome 独有的,但是谷歌正在推动标准化。 https://wicg.github.io/ScrollAnchoring/

根据滚动锚定文档,应用overflow-anchor: none到适当的元素将禁用滚动锚定调整。

更多信息:

  • https://github.com/WICG/ScrollAnchoring/blob/master/explainer.md https://github.com/WICG/ScrollAnchoring/blob/master/explainer.md
  • https://bugs.chromium.org/p/chromium/issues/detail?id=739860 https://bugs.chromium.org/p/chromium/issues/detail?id=739860
  • https://hacks.mozilla.org/2019/03/scroll-anchoring-in-firefox-66/ https://hacks.mozilla.org/2019/03/scroll-anchoring-in-firefox-66/
  • 更改 CSS 弹性顺序会导致滚动 https://stackoverflow.com/q/56050959/3597276
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Chrome 使用 Flexbox 视口锚定扩展方向 的相关文章

  • javascript:获取带有单位的CSS prop值[重复]

    这个问题在这里已经有答案了 我的代码是这样的 image 1 position absolute top 3vw 我的尝试 http jsfiddle net z8k6t3fb 1 http jsfiddle net z8k6t3fb 1
  • 没有宽度/高度的 SVG 以自然尺寸渲染

    我有这个 SVG 但没有width or height属性 我有以下 HTML div class block img src https s3 eu vAmfIxVv kiwi svg div 使用以下CSS block display
  • HTML5 安卓开发

    我对制作 Android 应用程序很感兴趣 而我的主要爱好是 Web 开发 现在让我困惑的是 人们用 HTML5 CSS3 JavaScript 语言制作应用程序 这些应用程序是在 Android 手机上的网络浏览器上运行还是像 Andro
  • CSS 中“!important”的反义词是什么?

    我正在构建一个 jQuery 插件 它使用 CSS 样式向嵌套 DIV 标签添加颜色和边距 由于我宁愿将插件保留为单个文件 因此我的代码使用 jQuery 将这些颜色和边距作为 CSS 属性直接添加到 DIV 中 css 方法 这些属性的值
  • 与现有表单完美集成的多文件上传器

    我知道这个问题可能太笼统了 但在花了一整天的时间思考这个问题之后 我对这个问题感到非常困惑 任何人都知道上传插件 那会与现有形式轻松集成 我尝试过的所有上传器插件 jQuery 文件上传 Plupload Uploadify Dropzon
  • 滚动在 chrome 中的 svg 异物内不起作用

    我在 svg 异物中有 div 带有溢出 auto 滚动仅适用于鼠标滚轮 无法拖动栏 它适用于 Firefox 但不适用于 chrome safari 如果 svg g 元素上没有转换 它就可以工作 参见小提琴 http jsfiddle
  • jQuery 可排序连接多个列表

    我有两个列表 每个列表中有 8 个列表元素 我想将任一元素拖动到任一列表中 并将两个列表的总顺序放在一起 目前 该顺序被归类为两个单独的可排序列表 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 不过我希望它是 显然按照元素
  • 如何将内部 div 与外部 div 的底部对齐?

    我需要将内部 div 与外部 div 的底部对齐 我的代码如下所示 div class myOuterDiv div class div1 floatLeft Variable content here div div class div2
  • JQuery:获取单选按钮值

    我有以下 HTML HTML
  • javaScript从单个值数组返回一个新的成对值数组[重复]

    这个问题在这里已经有答案了 可能的重复 将数组分割成块 https stackoverflow com questions 8495687 split array into chunks 我正在尝试将值数组转换为新的配对值数组 例如我需要转
  • 在 Bootstrap 3 中的工具提示中添加换行符

    我正在使用 Bootstrap 3 并向我的 div 添加了一个工具提示 我尝试过使用 n 和 创建新行 Bootstrap 会阻止我吗 div class content show tooltip 你需要添加data html true
  • 使用 javascript/jquery 检查 .css 样式表的名称

    我正在尝试为论坛制作一个小 chrome 扩展 但我只希望它在论坛的某个区域中工作 问题是我不能只做 matches subforum 因为该论坛中的线程无法通过 URL 区分它们所在的子论坛 subforum 有自己的 css 样式表 所
  • 缩放对象上的弹跳动画

    拥有对象比例 然后在返回到原始比例因子之前以该比例因子执行弹跳动画的最佳方法是什么 我意识到我可以做一些事情 比如将其缩放到 2 2 然后 1 8 然后 2 0 但我正在寻找一种方法 您只需在比例因子上执行弹跳动画 因为我的比例因子会改变
  • fancybox - 如何添加打开图像的链接?

    大伙计们有什么想法吗 我正在尝试链接 fancybox 中打开的图像 我到处都找遍了 听起来很简单 这是我正在使用的代码 a href img src example thumb png alt example a
  • 使用 CSS 网格布局跨越所有列/行的项目

    随着 CSS 网格布局模块很快在 Firefox 和 Chrome 中发布 我想我应该尝试了解如何使用它 我尝试用一 个项目创建一个简单的网格a跨越所有行的左侧 其他项目 b c d e等 跨越各个行的右侧 跨越行右侧的项目数量是可变的 因
  • Testaulous 无法启动 Chrome 浏览器

    当我在 浏览器 设置为 Firefox 的情况下运行 testaulous 时 会打开一个新的 Firefox 浏览器 我可以打开那里的控制台并查看我的 console log 消息等 当我在 浏览器 设置为 Chrome 或 Chrome
  • 在CSS中重置Margins/Padding时使用*是错误的吗?

    应该避免以下内容 还是应该因其简单性而受到赞扬 作为记录 我在我构建的每个站点中都使用它 但我注意到它并不存在于许多主流 CSS 重置框架中 他们是否也不使用它 margin 0 padding 0 最好不要使用它 因为它会导致表单元素出现
  • Mailto 链接在 Chrome 中不起作用,但在 Firefox 中有效?

    似乎是mailto我们在网站中嵌入的链接在 Chrome 中无法执行任何操作 但它们在 Firefox 中可以工作 简单的例子在这里 http jsfiddle net wAPNH http jsfiddle net wAPNH a hre
  • HTML 和 CSS 的基本编码标准 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想知道它们是否是像 PSR 01 这样的 HTML 和 CSS 基本编码标准 我尝试谷歌搜索和搜索 但没有找到 我建议看看类似的东西
  • fancybox iframe 尺寸

    在 fancybox 主页 http fancybox net home http fancybox net home 有一个示例 打开一个尺寸为屏幕 75 的 iFrame 我无法通过修改 js 文件上的宽度和高度属性来获取它 如网站上所

随机推荐

  • Javascript document.getSelection

    我正在尝试使用 document getSelection 来选择我在所见即所得编辑器的文本区域中输入的文本 但只有当您选择文本区域之外的文本时 它才有效 不知道有没有办法让它选择文本区域内的文本 下面是所见即所得文本编辑器的文本区域 您需
  • 从按分钟计算的原始数据中按小时聚合 MySQL 数据

    我有一个表 table 1 其中包含每分钟的数据 如下所示 date time value 2015 06 05 18 00 00 222 663 2015 06 05 18 01 00 222 749 2015 06 05 18 02 0
  • Mac系统上检测框架使用情况?

    我想在 OSX 上开发示例框架 并要求在任何时候该框架只能由单个客户端使用 我不知道如何实现这一点 他们有 API 来检测框架正在使用的天气吗 我们可以为此使用一些与文件相关的 API 吗 我看过一个 Windows 示例 其中使用以下命令
  • 在 jQuery 1.8 中的自定义过滤器选择器中获取“匹配”对象

    作为参考 这里有一篇文章使用 jQuery 创建自定义过滤器选择器 http answers oreilly com topic 1055 creating a custom filter selector with jquery 介绍 对
  • 大型文本文件中的词频

    我试图读取一个大文本文件并输出其中的不同单词及其计数 到目前为止 我已经尝试了几次 这是迄今为止我想出的最快的解决方案 private static readonly char separators public IDictionary
  • 为什么分配给空列表有效但分配给空元组无效?

    这出现在最近的 PyCon 演讲 https youtu be MCs5OvhV9S4 t 42m17s 该声明 没有做任何有意义的事情 但它也不会抛出异常 我感觉这一定是由于拆包规则造成的 你可以做元组拆包 http openbookpr
  • Amazon S3 区域转移? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 如何将特定区域的 S3 存储桶的内容传输到另一个特定区域的另一个 S3 存储桶 如果可以的话请提供最简单的方法和详细的步骤 您可以使用AWS 控制台
  • Python 中的yield 和C# 中的yield 之间的区别

    有什么区别yieldPython 中的关键字和yieldC 中的关键字 C s yield return相当于Python的yield and yield break只是return在Python中 除了这些细微的差别之外 它们的目的基本相
  • 智能垂直流线布局

    我正在 JPanel 布局 Swing 中寻找以下行为 基本上它会以垂直方式排列组件 一个组件彼此排列 当组件无法垂直放入容器中时 应将下一个组件添加到新行中 这将动态地继续 根据需要添加新行 添加 3 个标签后 它看起来像这样 label
  • Spring ControllerAdvice和认证/授权异常处理

    在我的 Spring Boot 应用程序中 我有以下 Web 安全配置 Configuration EnableWebSecurity public class WebSecurityConfiguration extends WebSec
  • SQL 对密码进行哈希处理

    我使用 SQL 语句手动从旧数据导入到 MSSQL 当我从原始文本密码散列到数据库中的 MVC 5 密码 HASH 字段时 我得到有趣的字符 如何将哈希插入 mvc5 身份表 MVC 5 表也使用 nvarchar select HASHB
  • 嵌套文档字符串的 Doctest

    假设我有以下代码 def foo s A dummy function foo For example gt gt gt a This is a test string line 1 This is a test string line 2
  • 如何将元素的属性添加到角度指令

    我是角度新手 我想编写一个指令 其中包含我在 html 中使用时添加到其中的所有属性 例如 这是我的指令 use strict app directive province function compile return restrict
  • 重写 Doctrine 2 继承中的 inversedBy 映射

    我有以下实体 class Restaurant OneToMany targetEntity CollectionTime mappedBy restaurant protected collectionTimes OneToMany ta
  • Google应用程序脚本根据一列删除重复项

    下面的代码非常棒 可以删除重复项 但我想更改一件事 例如 如果我有 A 列并且它包含重复值 而 B 列包含唯一值 在这种情况下 该函数不会从 A 列中删除重复项因为它如何将 A 列和 B 列连接在一起然后删除重复项 我需要的是仅根据 A 列
  • LISP:关键字参数,提供-p

    目前我正在学习 Peter Seibel 的 Practical Common Lisp 在 实用 一个简单的数据库 一章中 http www gigamonkeys com book practical a simple database
  • QT:QXmlStreamReader 始终返回“文档提前结束”错误

    我对 Qt QXmlStreamReader 有奇怪的问题 我正在尝试解析简单的文档 注意 它是使用 QXmlStreamWriter 生成的
  • 使用 AAD 将 SAML 交换为 JWT

    我有一个内部应用程序 允许用户使用 Azure AD 登录 身份验证时 将返回 SAML 断言 但是 应用程序进行的某些调用需要 JWT 用户登录时获取 JWT 的最佳方式是什么 或者有没有办法让我将 SAML 换成 JWT 免责声明 我不
  • EventSetter - Visual Studio 设计器中的 XAML 错误

    我已经使用 XAML 完成了 TreeView 但现在我想使用隐藏代码来管理事件 HierarchicalDataTemplate 包含一个图像 我需要捕获图像上的 MouseEnter MouseLeave 事件 我已经尝试过这样的方式
  • Google Chrome 使用 Flexbox 视口锚定扩展方向

    Google Chrome 中存在一个问题 当元素放置在带有adjacent弹性项目有space between or center合理的内容 这在 Firefox IE11 Edge 或 Safari 中不是问题 因为元素始终向下扩展 我