纯CSS绘制3D立方体

2022-10-17,,

本篇记录的是使用css3绘制3d立方体,并旋转起来。

 

我的思路:
1️⃣ 首先,用div元素画6个正方形摞在一起放在画布中间。为了区分,分别给每个div选择了不同的颜色,并且设置为半透明方便透视。
2️⃣ 将6个div元素分为三组(上下一组、左右一组、前后一组),想象以画布中心为圆点,使三组分别沿x/y/z轴旋转90度。
3️⃣ 上下一组,一张向上推50%正方形边长,一张向下推50%正方形边长;左右同理向左右推50%边长,前后同理向前后推50%边长。
4️⃣ 整体旋转展示。

 

※ html代码&css样式布局:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
border: 1px solid #cccccc;
}
 

.box {
position: relative;
width: 100px;
height: 100px;
margin: auto;
margin-top: 150px;
transform-style: preserve-3d;
}
 

.box div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: .5;
transform-origin: center;
}
 

.box div:nth-of-type(1) {
 
}
 

.box div:nth-of-type(2) {
background-color: yellow;
}
 

.box div:nth-of-type(3) {
background-color: green;
}
 

.box div:nth-of-type(4) {
background-color: blue;
}
 

.box div:nth-of-type(5) {
background-color: black;
}
 

.box div:nth-of-type(6) {
background-color: darkmagenta;
}
</style>
</head>
<body>
 

<div class="container">
<div class="box animate">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
</body>
</html>

  

 

※ css3添加6个正方形的动画效果

.box.animate div {
animation: ease 4s 0s infinite;
}
 

.box.animate div:nth-of-type(1) {
animation-name: box1-move;
}
 

.box.animate div:nth-of-type(2) {
animation-name: box2-move;
}
 

.box.animate div:nth-of-type(3) {
animation-name: box3-move;
}
 

.box.animate div:nth-of-type(4) {
animation-name: box4-move;
}
 

.box.animate div:nth-of-type(5) {
animation-name: box5-move;
}
 

.box.animate div:nth-of-type(6) {
animation-name: box6-move;
}
 

@keyframes box1-move {
0% {
transform: rotatex(0deg);
}
25% {
transform: rotatex(90deg);
}
50% {
transform: rotatex(90deg) translatez(50px);
}
100% {
transform: rotatex(90deg) translatez(50px);
}
}
 

@keyframes box2-move {
0% {
transform: rotatex(0deg);
}
25% {
transform: rotatex(90deg);
}
50% {
transform: rotatex(90deg) translatez(-50px);
}
100% {
transform: rotatex(90deg) translatez(-50px);
}
}
 

@keyframes box3-move {
0% {
transform: rotatex(0deg);
}
25% {
transform: rotatey(90deg);
}
50% {
transform: rotatey(90deg) translatez(50px);
}
100% {
transform: rotatey(90deg) translatez(50px);
}
}
 

@keyframes box4-move {
0% {
transform: rotatex(0deg);
}
25% {
transform: rotatey(90deg);
}
50% {
transform: rotatey(90deg) translatez(-50px);
}
100% {
transform: rotatey(90deg) translatez(-50px);
}
}
 

@keyframes box5-move {
0% {
 

}
25% {
transform: translatez(0px);
}
50% {
transform: translatez(50px);
}
100% {
transform: translatez(50px);
}
}
 

@keyframes box6-move {
0% {
 

}
25% {
transform: translatez(0px);
}
50% {
transform: translatez(-50px);
}
100% {
transform: translatez(-50px);
}
}

  

 

※ 添加整提旋转动画

.box.animate {
animation: box-move ease 4s 0s infinite;
}
 

@keyframes box-move {
0% {
transform: rotatex(0deg) rotatey(0deg)
}
50% {
transform: rotatex(45deg) rotatey(45deg)
}
100% {
transform: rotatex(405deg) rotatey(405deg)
}
}

  

 

动画转的我有点头晕

《纯CSS绘制3D立方体.doc》

下载本文的Word格式文档,以方便收藏与打印。