3D盒子效果以及遇到的bug
阿新 • • 發佈:2018-12-10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
perspective: 1200px;
background: gray;
}
ul{
padding: 0;
list-style: none;
width: 200px;
height: 200px;
margin: 400px auto;
position: relative;
transition: all 20s;
transform-style : preserve-3d;
/* 在ul這裡加上opacity會有bug */
/* opacity: 0.9; */
}
ul:hover {
transform: rotateY(2700deg) rotateX(2700deg);
}
li {
width: 100%;
height: 100%;
position: absolute;
opacity: .7;
}
li:nth-child(1) {
transform-origin: right;
left: -180px;
background-color: #f00;
transform: rotateY(90deg);
}
li:nth-child(2) {
top: -180px;
background-color: #0f0;
transform-origin: bottom;
transform: rotateX(-90deg);
}
li:nth-child(3) {
right: -180px;
background-color: #00f;
transform-origin: left;
transform: rotateY(-90deg);
}
li:nth-child(4){
bottom: -180px;
background-color: hotpink;
transform-origin: top;
transform: rotateX(90deg);
}
li:nth-child(5){
background-color: purple;
transform: translateZ(180px);
}
li:nth-child(6){
background-color: yellowgreen;
}
</style>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
- 頁面佈局:首先弄一個ul,放6個li標籤,利用定位將他們放在合適的位置上。
- 利用transform-origin定軸的位置,然後用transform的rotate旋轉至合適的位置,正面的使用平移transform的translateZ即可
- 最後給ul加上一個旋轉看看立體效果,要注意給body加上視距以及在ul上開啟3d效果
bug以及知識點: 知識點:
- 如果你做了一個立體,那麼立體整體翻轉的時候是沒有3D效果的,因為瀏覽器預設沒有開啟3D效果,要開啟3D preserve-3d: 指定子元素定位在三維空間內
Bug: 1.我這個案例是想讓每個面都透明的,那麼我就想到加opacity屬性來實現,本來想加在li標籤上,結果發現錯加在ul上了,然後就發現了個bug,只要在ul上加了opacity,我的3d佈局就全沒了,通過transform旋轉的會沒有高度或者寬度,有興趣的朋友可以試著把上述程式碼註釋一下看看效果。