要让.box
在.cover
之上显示,但.case
被.cover
遮罩,可以通过调整它们的CSS样式和定位来实现。以下是具体的步骤和代码示例:
- 设置定位:确保
.cover
、.case
和.box
都设置为定位元素(例如使用position: fixed
或position: absolute
),因为z-index属性只对定位元素有效。 - 调整z-index值:通过设置不同的z-index值来控制元素的层叠顺序。具有较高z-index值的元素将显示在具有较低z-index值的元素之上。
- 给
.cover
设置一个较低的z-index值,例如z-index: 999;
。 - 给
.case
设置一个比.cover
略高的z-index值,例如z-index: 1000;
。这样可以确保.case
部分显示在.cover
之上。 - 给
.box
设置一个更高的z-index值,例如z-index: 99999;
。这样可以使.box
显示在.case
和.cover
之上。
- 示例代码:
<style>
.container {
height: 100vh;
background: #000;
position: relative;
}
.cover {
position: fixed;
background: rgba(255, 255, 255, 0.8);
width: 100%;
height: 100%;
left: 0;
bottom: 0;
z-index: 999; /* 较低的z-index */
}
.case {
position: fixed;
z-index: 1000; /* 比.cover的z-index高 */
width: 800px;
height: 400px;
left: 50%;
top: 50%;
margin-left: -400px;
margin-top: -200px;
background: #fff;
display: flex;
}
.box {
width: 300px;
height: 200px;
background: #000;
position: fixed;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;
z-index: 99999; /* 更高的z-index */
}
</style>
总的来说,通过以上方法,可以实现.box
在.cover
之上显示,而.case
则被.cover
遮罩的效果。