javascript - 为什么 will-change :opacity treat fixed elements differently than will-change:transform in chrome?

标签 javascript html css web-performance will-change

我正在尝试优化我的网络应用程序的滚动。我有包含大量数据的数据表,滚动变得非常糟糕。我将 will-change: transform 添加到数据表中,但它破坏了我的 position: fixed 表头(我将它们固定为允许它们随视口(viewport)滚动) .元素根本不随视口(viewport)移动,它们只是停留在文档流中。

但我偶然发现,如果我改用 will-change:opacity,我的固定 header 就可以了。有人可以解释这种行为吗?我还没有找到任何说明他们应该采取不同行动的文件。

这是一个代码笔,其中包含我正在谈论的示例。在值之间切换,并在蓝色 div 中滚动。 https://codepen.io/bkfarns/pen/aLYgrN

这也是笔的基本代码:

html:

  <div class="container">
    <div class="fixed">should be position: fixed</div>
    <div class="too-tall">div that is too tall</div>
  </div>

CSS:

.container {
    margin-left: 100px;
    background-color: blue;
    width:400px;
    height:300px;
    overflow: auto;
    will-change: transform;//changing this to opacity fixes the issue
}

.fixed {
  background-color: grey;
  position: fixed;
  margin-left: 150px;
  margin-top: 100px;
}

.too-tall {
  background-color: red;
  width: 90px;
  height: 600px;
}

最佳答案

will-change 的全部要点是,当指定的属性提前更改时,浏览器必须应用所有可能的更改,从而减少更改本身所需的时间。实际上,这意味着通过指定 will-change:transform 可以使元素发生转换(尽管在视觉上它保持在同一位置),并且转换后的元素的后代不能根据 CSS Transforms spec 进行修复。 .不透明度没有这样的效果,所以 will-change:opacity 不会破坏固定定位。

另外,will-change 本身并没有任何“优化魔法”,它只是优化指定属性的变化。一些属性强制将元素添加到理论上可以由 GPU 更有效地处理的复合层,但如果此类元素太多,则可能会产生相反的效果。为了优化滚动,可能是 other strategies会更有效率。

关于javascript - 为什么 will-change :opacity treat fixed elements differently than will-change:transform in chrome?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46656617/

相关文章:

html - 如何用Bootstrap实现流体-固定-流体三列布局?

javascript - 表单发布不接受链接

html - Web 抓取后表数据返回空值

html - 响应式图像和文本行

css - HTML5 Boilerplate 在网站底部留下一个空白

html - 我怎样才能正确地将它变成图像边框?

javascript - 获取元素的宽度和高度,并将其设为 JavaScript 变量

javascript - 具有向 Apple AppStore 提交 HTML/JS 应用程序(例如 jQuery Mobile 和 PhoneGap)的经验

javascript - 从第一个背景图像到第二个背景图像 5 秒后加载淡入

javascript - 如何在 PHP if/else 语句中调用 JavaScript 文件?