🎲 3D 立方体实现教程
一步一步学习如何创建一个完整的 3D 立方体
最终效果预览
这是我们要实现的 3D 立方体效果,可以自动旋转并展示所有六个面:
核心概念
在开始之前,我们需要理解三个关键的 CSS 属性:
1. perspective(透视)
设置观察者与 3D 场景的距离,创造近大远小的透视效果。
perspective: 800px;
2. transform-style
让子元素在 3D 空间中渲染,而不是被扁平化。
transform-style: preserve-3d;
3. transform(变换)
对元素进行 3D 变换,包括旋转、平移、缩放等。
transform: rotateY(90deg) translateZ(100px);
步骤 1:创建 HTML 结构
我们需要三层结构:外层容器 → 立方体容器 → 六个面
<div class="perspective-container">
<div class="cube">
<div class="cube-face face-front">前</div>
<div class="cube-face face-back">后</div>
<div class="cube-face face-right">右</div>
<div class="cube-face face-left">左</div>
<div class="cube-face face-top">上</div>
<div class="cube-face face-bottom">下</div>
</div>
</div>
为什么需要三层?
- 外层容器:设置 perspective(透视点)
- 立方体容器:设置 transform-style: preserve-3d(3D 空间)
- 六个面:应用具体的 3D 变换
步骤 2:设置外层容器和透视
透视值决定了 3D 效果的强度,值越小,透视效果越强烈。
.perspective-container {
width: 200px;
height: 200px;
perspective: 800px; /* 设置透视距离 */
}
透视值的影响:
- 400px - 强烈的透视效果(夸张)
- 800px - 适中的透视效果(推荐)
- 1200px - 轻微的透视效果(平缓)
步骤 3:设置立方体容器
这是最关键的一步,让子元素在 3D 空间中渲染。
.cube {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d; /* 关键!启用 3D 空间 */
transition: transform 1s;
}
⚠️ 重要:如果不设置
transform-style: preserve-3d,所有子元素会被扁平化,看不到 3D 效果!
步骤 4:设置立方体面的基础样式
所有面共享相同的基础样式,使用绝对定位重叠在一起。
.cube-face {
position: absolute; /* 绝对定位,重叠在一起 */
width: 200px;
height: 200px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5rem;
font-weight: 700;
color: white;
opacity: 0.9;
border: 2px solid rgba(255, 255, 255, 0.3);
}
步骤 5:定位六个面(核心)
💡 核心概念:坐标系统
首先理解 3D 坐标系:
- X 轴:水平方向(左 ← → 右)
- Y 轴:垂直方向(上 ↑ ↓ 下)
- Z 轴:深度方向(后 ⊙ → 前,指向你的眼睛)
🔑 为什么是边长的一半?
想象一下:
- 所有 6 个面最初都重叠在中心点(position: absolute)
- 立方体边长是 200px,中心到边缘的距离就是 200px ÷ 2 = 100px
- 所以每个面需要从中心向外"推"100px,才能形成立方体
类比:就像 6 张纸叠在一起,你需要把每张纸向不同方向推开 100px,才能组成一个盒子。
🔄 旋转轴的选择规则
记住这个简单规则:
- rotateX:绕 X 轴旋转 = 上下翻转(像翻书)→ 用于上面和下面
- rotateY:绕 Y 轴旋转 = 左右转动(像开门)→ 用于左面、右面、后面
- 前面:不需要旋转,它本来就面向你
变换的两步顺序(重要!):
- 第一步:旋转到正确的方向(rotateX/rotateY)
- 第二步:平移到正确的位置(translateZ)
⚠️ 顺序不能反!如果先平移再旋转,面会飞到错误的位置。
📍 前面(Front)
思路:前面本来就面向你(Z 轴正方向),不需要旋转,直接向前推 100px。
.face-front {
background: #10b981;
transform: translateZ(100px);
/* 不需要旋转,直接向前(Z轴+)移动 100px */
}
📍 后面(Back)
思路:后面需要转 180° 背对你,然后向"它的前方"推 100px(实际是向后)。
💡 为什么用 rotateY?因为是左右转动(像转身),不是上下翻。
.face-back {
background: #059669;
transform: rotateY(180deg) translateZ(100px);
/* 1. 绕 Y 轴旋转 180°(转身背对你)*/
/* 2. 向前推 100px(此时的"前"是原来的"后")*/
}
📍 右面(Right)
思路:右面需要向右转 90°,然后向前推 100px。
💡 为什么是 90°?从正面转到右侧,刚好是 90° 的角度。
.face-right {
background: #34d399;
transform: rotateY(90deg) translateZ(100px);
/* 1. 绕 Y 轴向右旋转 90° */
/* 2. 向前推 100px */
}
📍 左面(Left)
思路:左面需要向左转 90°(负角度),然后向前推 100px。
💡 为什么是 -90°?负数表示反方向,向左转就是 -90°。
.face-left {
background: #065f46;
transform: rotateY(-90deg) translateZ(100px);
/* 1. 绕 Y 轴向左旋转 -90° */
/* 2. 向前推 100px */
}
📍 上面(Top)
思路:上面需要向上翻 90°,然后向前推 100px。
💡 为什么用 rotateX?因为是上下翻转(像翻书),不是左右转。
.face-top {
background: #6ee7b7;
transform: rotateX(90deg) translateZ(100px);
/* 1. 绕 X 轴向上翻转 90° */
/* 2. 向前推 100px */
}
📍 下面(Bottom)
思路:下面需要向下翻 90°(负角度),然后向前推 100px。
💡 为什么是 -90°?向下翻是负方向,所以用 -90°。
.face-bottom {
background: #047857;
transform: rotateX(-90deg) translateZ(100px);
/* 1. 绕 X 轴向下翻转 -90° */
/* 2. 向前推 100px */
}
📝 总结记忆口诀
- 前面:不转直接推(translateZ)
- 后面:转身 180°(rotateY 180°)
- 左右面:左右转门(rotateY ±90°)
- 上下面:上下翻书(rotateX ±90°)
- 所有面:最后都向前推 100px(translateZ 100px)
步骤 6:添加旋转动画
使用 CSS 动画让立方体持续旋转,展示所有面。
/* 定义旋转动画 */
@keyframes rotateCube {
from {
transform: rotateX(0deg) rotateY(0deg);
}
to {
transform: rotateX(360deg) rotateY(360deg);
}
}
/* 应用动画到立方体 */
.cube {
animation: rotateCube 8s infinite linear;
/* 8秒完成一次旋转,无限循环,匀速 */
}
关键要点总结
✅ 必须记住的三个要点
- 三层结构:外层设置 perspective → 中层设置 transform-style: preserve-3d → 内层应用 transform
- 变换顺序:先旋转(rotate)再平移(translateZ),顺序不能颠倒
- 平移距离:translateZ 的值是立方体边长的一半(200px ÷ 2 = 100px)
🎯 常见问题
- 看不到 3D 效果?检查是否设置了 transform-style: preserve-3d
- 立方体变形?检查 perspective 值是否合适(推荐 600-1000px)
- 面的位置不对?检查 transform 的顺序,应该是 rotate 在前,translateZ 在后
- 看到背面?可以添加 backface-visibility: hidden 隐藏背面
扩展练习
掌握了基础后,可以尝试以下扩展:
- 🎨 修改立方体的大小(记得同步修改 translateZ 的值)
- 🖼️ 在每个面上添加图片或内容
- 🎮 添加鼠标拖拽控制旋转
- ✨ 添加不同的动画效果(弹跳、缩放等)
- 🎲 创建骰子效果(点击随机旋转到某一面)
- 📦 创建多个立方体组成的场景
相关教程
深入学习 3D 立方体涉及的核心概念: