我正在同一3D空间中创建3D立方体,但是由于渲染顺序,不同盒子的侧面相互夹住。
在视差类中设置了透视图,我在每个视差框中都保留了3D效果。这些框内的4个Div组成了3D框的侧面。
两个盒子都构成了立方体的正面,并包含了立方体的侧面的另外四个壁。
示例:
body {
overflow: hidden;
background: gray;
margin: 0px;
}
/* set the position of box1 and box2 */
#box1 {
grid-column: 6/7;
}
#box2 {
grid-column: 7/8;
}
/*create a sqared grid*/
#grid {
padding: 100px;
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-auto-rows: calc(100vw / 8);
}
/* CSS 3D classes*/
/*Adds the 3D effect to the scene*/
.parallax {
perspective: 1000px;
perspective-origin: 50vw 50vh;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
}
/*holds all the walls together to create a cube*/
.parallax-box {
transform-style: preserve-3d;
position: relative;
width: 100%;
height: 100%;
background-color: white;
transition: 200ms;
}
/*Effect for seeing the clipping better*/
.parallax-box:hover {
transform: translateZ(-25px);
}
.parallax-box div {
position: absolute;
background-color: rgb(200, 200, 200);
width: 50px;
height: 50px;
}
/*rotate walls so a box is created*/
.parallax-box div:nth-child(1) {
transform: rotate3d(-1, 0, 0, 90deg);
transform-origin: top;
width: inherit;
top: 0;
}
.parallax-box div:nth-child(2) {
transform: rotate3d(1, 0, 0, 90deg);
transform-origin: bottom;
width: inherit;
bottom: 0;
}
.parallax-box div:nth-child(3) {
transform: rotate3d(0, 1, 0, 90deg);
transform-origin: left;
height: inherit;
left: 0;
}
.parallax-box div:nth-child(4) {
transform: rotate3d(0, -1, 0, 90deg);
transform-origin: right;
height: inherit;
right: 0;
}
<!-- creates a grid and the 3D effect -->
<div id="grid" class="parallax">
<div id="box1" class="parallax-box">
<div></div><div></div><div></div><div></div>
</div>
<!-- the second box which walls are visible through the 1st box -->
<div id="box2" class="parallax-box">
<div></div><div></div><div></div><div></div>
</div>
<!-- makes the window scrollable -->
<div style="height: 200vh;"></div>
</div>