gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:29:51 25 4
gpt4 key购买 nike

我正在尝试优化我的网络应用程序的滚动。我有包含大量数据的数据表,滚动变得非常糟糕。我将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com