老师关于复合图层的问题
来源:3-2 回流与重绘, 如何避免布局抖动

hy_wang
2021-02-01
老师这里我还是不太明白,老师讲到设置transform
+willchange
可以触发复合图层从而不进行重绘和回流。并且即使修改高度和宽度也只会触发自己图层的重绘。
请问下老师如果使用transform:translateZ(0)
是不是就可以省略will-change
?
以及老师我这段代码,蓝色div明明已经使用了will-change+transform,我理解他已经和红色div并不在一个layout
中了,之后再进行蓝色div的动画的使用我观察到红色div
所在的图层的确也发生了回流,请问老师这是不是有问题。。这里因为的确想搞明白但是感觉网上也没有什么好的文章。希望老师可以解惑,如果可以分享一些这方面精细文章那最好不过了。谢谢老师
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 200px;
height: 200px;
background-color: blue;
will-change: transform;
animation: wang 1s infinite;
}
@keyframes wang {
from {
transform: translate(0px);
width: 0px;
height: 0;
}
to {
transform: translate(100px);
height: 100px;
width: 300px;
}
}
.app1 {
height: 300px;
width: 300px;
background-color: red;
}
</style>
</head>
<body>
<div id="app" class="div">
</div>
<div class="app1"></div>
<script>
const app = document.getElementById('app')
</script>
</body>
</html>
写回答
1回答
-
Mr_Max
2021-02-02
同学你好!
1. translateZ也可以。
2. 蓝色的动画在硬件加速的独立图层,不进行重绘;红色不在硬件加速的独立图层,会发生重绘。
3. 优化是对一些高消耗的动画元素进行的,只有被优化的元素才会有优化后的效果。
其实这方面的文章非常的多,推荐你看一下这篇:https://juejin.cn/post/6844903618982723592
00
相似问题