純CSS繪制3D立方體


本篇記錄的是使用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)
}
}

  

 

動畫轉的我有點頭暈🤦‍♀️,所以我決定把.animate類名剝離出來,用JavaScript通過按鈕觸發的模式將.animate添加到DOM中去,這樣,只有點擊按鈕后動畫才會被觸發。最后,我添加了兩個按鈕,move和stop,分別用來觸發動畫和使動畫停止。
<!-- Html代碼 -->
<div class="ope">
<button id="animate">Move</button>
<button id="stop">Stop</button>
</div>
 

<!-- CSS代碼 -->
.ope {
margin-top: 100px;
text-align: center;
}
 

.ope button {
margin: 0 10px;
border: 1px solid #4380f5;
border-radius: 5px;
cursor: pointer;
background-color: #4380f5;
color: #ffffff;
outline: none;
}
 

.ope button:hover {
background-color: #3e76e3;
}
 

.ope button:active {
background-color: #3361ba;
}
 
<!-- JavaScript代碼 -->
<script>
(function () {
var box = document.getElementsByClassName('box')[0];
 

document.getElementById('animate').onclick = function () {
box.className = 'box animate';
}
document.getElementById('stop').onclick = function () {
box.className = 'box';
}
})();
</script>

 

 更多文章可移步到我的github博客,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM