簡稱3維座標系 比二維座標系多一個Z軸
3D位移在2D的基礎上多一個可移動的Z軸
transform: translate3d(x,y,z);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } .box { width: 200px; height: 200px; background-color: blue; /* 位移3D的寫法 */ /* transform: translate3d(200px, 200px, 200px); */ /* 或者能夠這樣寫 */ transform: translateX(400px) translateY(400px) translateZ(400px); } </style> </head> <body> <div class="box"></div> </body> </html>
注意事項html
特色瀏覽器
在這張圖中,d爲透視,位於人的眼睛和被觀察物體的中間,即近大遠小動畫
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } body { perspective: 500px; } .box { width: 200px; height: 200px; margin: 200px auto; background-color: blue; transform: translateZ(100px); } </style> </head> <body> <div class="box"></div> </body> </html>
3D 旋轉指能夠讓元素在三維平面內沿着 x 軸、y 軸、z 軸 或者自定義軸進行旋轉
左手彎曲的手指方向即爲x軸的正方向spa
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } body { /* 3D效果的實現,必須依賴於透視 */ perspective: 500px; } img { display: block; margin: 200px auto; /* 設置圓角邊框 */ border-radius: 50%; /* 使用動畫 */ animation: move 2s linear 0s infinite alternate; /* 讓元素居中對齊 */ margin: 200px auto; } /* 定義動畫 */ @keyframes move { form { transform: rotateX(0deg); } to { transform: rotateX(360deg); } } </style> </head> <body> <img src="./image/抽象.jpg" alt=""> </body> </html>
左手手指彎曲的方向即爲y軸的正方向3d
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } body { /* 加此屬性是爲了更好的使用3d效果 */ perspective: 500px; } img { /* 轉換成塊級元素 */ display: block; /* 移動的中心位置 */ margin: 200px auto; /* 使用動畫 */ animation: move 2s linear 0s infinite alternate; } /* 定義動畫 */ @keyframes move { 0% { transform: rotateY(0); } 100% { transform: rotateY(180deg); } } </style> </head> <body> <img src="./image/星空.jpg" alt=""> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } img { /* 轉換成塊級元素*/ display: block; margin: 200px auto; /* 使用動畫 */ animation: move 2s linear 0s infinite alternate; } /* 定義動畫 */ @keyframes move { 0% { transform: rotateZ(0deg); } 100% { transform: rotateZ(360deg); } } </style> </head> <body> <img src="./image/四.jpg" alt=""> </body> </html>
transform: rotate3d(1, 1, 0, 180deg)
-- 沿着對角線旋轉 45degtransform: rotate3d(1, 0, 0, 180deg)
-- 沿着 x 軸旋轉 45degtransform:rotate3d(x,y,z,deg)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } img { display: block; width: 600px; margin: 200px auto; transition: all 2s linear 0s; } img:hover { transform: rotate3d(1, 1, 1, 180deg) } </style> </head> <body> <img src="./image/星空.jpg" alt=""> </body> </html>
transform:preserve-3d 讓子元素保持3d空間環境
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } body { perspective: 500px; } .box { /* 開啓定位 */ position: relative; width: 400px; height: 400px; margin: 200px auto; /* 父元素開啓3d環境 */ transform-style: preserve-3d; animation: clinopodium 2s linear 0s infinite alternate; } /*使用屬性選擇器設置樣式 */ div[class^=son] { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: pink; } /* 設置子元素的第二個樣式 */ .box div:nth-of-type(2) { background-color: blue; transform: rotateX(112deg); } .box div:nth-of-type(3) { background-color: purple; transform: rotateX(230deg); } /* 定義動畫 */ @keyframes clinopodium { 0% { transform: rotatex(0deg); } 100% { transform: rotateX(360deg); } } </style> </head> <body> <div class="box"> <div class="son1"></div> <div class="son2"></div> <div class="son3"></div> </div> </body> </html>
思路code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body { perspective: 400px; } .box { position: relative; width: 400px; height: 400px; margin: 200px auto; transition: all 0.4S; /* 開啓子元素在3d空間的環境 */ transform-style: preserve-3d; } .box:hover { transform: rotateY(180deg); } .front, .back { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; line-height: 400px; color: #cccccc; font-weight: 700; border-radius: 50%; } .front { background-color: pink; z-index: 1; } .back { background-color: purple; /* 像手機同樣 背靠背 旋轉 */ transform: rotateY(180deg); } </style> </head> <body> <div class="box"> <div class="front">堯子陌</div> <div class="back">臨風笑卻世間</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> /* 注意 視距寫在li上面 */ * { margin: 0; padding: 0; } ul { width: 600px; margin: 100px auto; } li { /* 設置定位 子絕父相 */ float: left; width: 100px; height: 35px; list-style: none; perspective: 500px; } .box { position: relative; top: 0; left: 0; width: 100%; height: 100%; /* 開啓子元素的 3d效果 */ transform-style: preserve-3d; /* 使用過渡效果 */ transition: all 0.4s linear 0s; } /* 鼠標通過box旋轉90deg */ .box:hover { transform: rotateX(90deg); } .front, .bottom { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; line-height: 35px; color: cornsilk; font-family: '楷體'; } .front { background-color: blue; z-index: 1; transform: translateZ(17.5px); } .bottom { background-color: brown; /* 先向下移動17.5px 再沿X軸旋轉90deg */ transform: translateY(17.5px) rotateX(-90deg); } </style> </head> <body> <ul> <li> <div class="box"> <div class="front">hello</div> <div class="bottom">堯子陌</div> </div> </li> <li> <div class="box"> <div class="front">hello</div> <div class="bottom">堯子陌</div> </div> </li> </ul> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } body { /* 視距 */ perspective: 1000px; } section { position: relative; width: 200px; height: 200px; margin: 200px auto; /* 讓子元素顯示3d屬性 */ transform-style: preserve-3d; /* 使用動畫 */ animation: rotate 2s linear 0s infinite; } section:hover { animation-play-state: paused; } @keyframes rotate { 0% { transform: rotateY(0deg); } 100% { transform: rotateY(360deg); } } section div { position: absolute; top: 0; left: 0; overflow: hidden; width: 100%; height: 100%; border-radius: 50px; } section div:nth-of-type(1) { transform: rotateY(0deg) translateZ(300px); } section div:nth-of-type(2) { transform: rotateY(60deg) translateZ(300px); } section div:nth-of-type(3) { transform: rotateY(120deg) translateZ(300px); } section div:nth-of-type(4) { transform: rotateY(180deg) translateZ(300px); } section div:nth-of-type(5) { transform: rotateY(240deg) translateZ(300px); } section div:nth-of-type(6) { transform: rotateY(300deg) translateZ(300px); } </style> </head> <body> <section> <div><img src="./image/一.jpg" alt=""></div> <div><img src="./image/二.jpg" alt=""></div> <div><img src="./image/三.jpg" alt=""></div> <div><img src="./image/四.jpg" alt=""></div> <div><img src="./image/五.jpg" alt=""></div> <div><img src="./image/六.jpg" alt=""></div> </section> </body> </html>