- 1.什么是2D和3D
- 2D就是一個(gè)平面, 只有寬度和高度, 沒(méi)有厚度
- 3D就是一個(gè)立體, 有寬度和高度, 還有厚度
- 默認(rèn)情況下所有的元素都是呈2D展現(xiàn)的
- 2.如何讓某個(gè)元素呈3D展現(xiàn)
- 和透視一樣, 想看到某個(gè)元素的3d效果, 只需要給他的
父元素
添加一個(gè)transform-style屬性, 然后設(shè)置為preserve-3d即可
- 和透視一樣, 想看到某個(gè)元素的3d效果, 只需要給他的
3D轉(zhuǎn)換模塊-正方體
示例代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>107-3D轉(zhuǎn)換模塊-正方體</title>
<style>
*{
margin: 0;
padding: 0;
}
ul{
width: 200px;
height: 200px;
border: 1px solid #000;
box-sizing: border-box;
margin: 100px auto;
position: relative;
transform: rotateY(0deg) rotateX(0deg);
transform-style: preserve-3d;
}
ul li{
list-style: none;
width: 200px;
height: 200px;
font-size: 60px;
text-align: center;
line-height: 200px;
position: absolute;
left: 0;
top: 0;
}
ul li:nth-child(1){
background-color: red;
transform: rotateX(90deg) translateZ(100px);
}
ul li:nth-child(2){
background-color: green;
transform: rotateX(180deg) translateZ(100px);
}
ul li:nth-child(3){
background-color: blue;
transform: rotateX(270deg) translateZ(100px);
}
ul li:nth-child(4){
background-color: yellow;
transform: rotateX(360deg) translateZ(100px);
}
ul li:nth-child(5){
background-color: purple;
transform: translateX(-100px) rotateY(90deg);
}
ul li:nth-child(6){
background-color: pink;
transform: translateX(100px) rotateY(90deg);
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</body>
</html>
正方體輪播圖可以參考以上代碼實(shí)現(xiàn)
長(zhǎng)方體
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>109-3D轉(zhuǎn)換模塊-長(zhǎng)方體</title>
<style>
*{
margin: 0;
padding: 0;
}
/*
.father{
width: 200px;
height: 200px;
border: 1px solid #000;
margin: 100px auto;
}
.son{
width: 200px;
height: 200px;
background-color: rgba(255,0,0,0.3);
transform: scale(1.5, 1);
}
*/
ul{
width: 200px;
height: 200px;
border: 1px solid #000;
box-sizing: border-box;
margin: 100px auto;
position: relative;
transform: rotateY(0deg) rotateX(0deg);
transform-style: preserve-3d;
}
ul li{
list-style: none;
width: 200px;
height: 200px;
font-size: 60px;
text-align: center;
line-height: 200px;
position: absolute;
left: 0;
top: 0;
}
ul li:nth-child(1){
background-color: red;
transform: rotateX(90deg) translateZ(100px) scale(2, 1);
}
ul li:nth-child(2){
background-color: green;
transform: rotateX(180deg) translateZ(100px) scale(2, 1);
}
ul li:nth-child(3){
background-color: blue;
transform: rotateX(270deg) translateZ(100px) scale(2, 1);
}
ul li:nth-child(4){
background-color: yellow;
transform: rotateX(360deg) translateZ(100px) scale(2, 1);
}
ul li:nth-child(5){
background-color: purple;
transform: translateX(-200px) rotateY(90deg);
}
ul li:nth-child(6){
background-color: pink;
transform: translateX(200px) rotateY(90deg);
}
</style>
</head>
<body>
<!--
<div class="father">
<div class="son"></div>
</div>
-->
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</body>
</html>