浏览器工作原理相关的文章中介绍浏览器渲染进程生成图层树,拥有层叠上下文属性的元素会被提升为单独的一层。
但在浏览器中运行Demo,DevTools Layers中却并没有出现元素分层结果。
Demo代码如下:
<!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>
.box1, .box2 {
width: 200px;
height: 200px;
position: absolute;
}
.box1 {
background-color: red;
z-index: 1;
}
.box2 {
background-color: blue;
z-index: 0;
left: 100px;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
尝试使用position:sticky
配合left: 100px
,确实会出现分层。
猜想是浏览器不会处理所有层叠上下文?