--- tags: CSS,mobile,第二章 --- # CSS mobile 第二章 ## 空間轉換 ### 位移 ![](https://i.imgur.com/Nvg00h3.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 200px; height: 200px; margin: 100px auto; background-color: pink; transition: all 0.5s; } .box:hover { transform: translate3d(50px, 100px, 200px); } </style> </head> <body> <div class="box"></div> </body> </html> ``` ### 透視 ![](https://i.imgur.com/Sz5xmY3.jpg) ![](https://i.imgur.com/1veFPTZ.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { perspective: 1000px; } .box { width: 200px; height: 200px; margin: 100px auto; background-color: pink; transition: all 0.5s; } .box:hover { transform: translateZ(200px); } </style> </head> <body> <div class="box"></div> </body> </html> ``` ### rotateZ ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 720px; margin: 100px auto; } img { width: 720px; transition: all 2s; } .box img:hover { transform: rotateZ(360deg); } </style> </head> <body> <div class="box"> <img src="./p015image/rotate.jpg" alt="" /> </div> </body> </html> ``` ### rotateX ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 720px; margin: 100px auto; perspective: 1000px; } img { width: 720px; transition: all 2s; } .box img:hover { transform: rotateX(60deg); } </style> </head> <body> <div class="box"> <img src="./p015image/rotate.jpg" alt="" /> </div> </body> </html> ``` ### rotateY ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 720px; margin: 100px auto; perspective: 1000px; } img { width: 720px; transition: all 2s; } .box img:hover { transform: rotateY(60deg); } </style> </head> <body> <div class="box"> <img src="./p015image/rotate.jpg" alt="" /> </div> </body> </html> ``` ### rotate3D 99.99%工作用不到 ![](https://i.imgur.com/Z20j8IL.jpg) ### 立體呈現 ![](https://i.imgur.com/kSmcX7K.jpg) ![](https://i.imgur.com/nuGaOys.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .cube { position: relative; width: 200px; height: 200px; margin: 100px auto; background-color: pink; transition: all 2s; transform-style: preserve-3d; } .cube div { position: absolute; left: 0; top: 0; width: 200px; height: 200px; } .front { background-color: orange; transform: translateZ(200px); } .back { background-color: green; } .cube:hover { transform: rotateY(180deg); } </style> </head> <body> <div class="cube"> <div class="front">前面</div> <div class="back">後面</div> </div> </body> </html> ``` ### 3D導航案例 ![](https://i.imgur.com/uk8gwhq.jpg) ![](https://i.imgur.com/ULVy9DJ.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> ul { margin: 0; padding: 0; list-style: none; } .navs { width: 300px; height: 40px; margin: 50px auto; } .navs li { float: left; width: 100px; height: 40px; line-height: 40px; transition: all 0.5s; transform-style: preserve-3d; /* 觀察時使用 */ /* transform: rotateX(-20deg) rotateY(30deg); */ } .navs li a { position: absolute; left: 0; top: 0; display: block; width: 100%; height: 100%; text-align: center; text-decoration: none; color: white; } .navs li a:first-child { background-color: green; transform: translateZ(20px); } .navs li a:last-child { background-color: orange; transform: rotateX(90deg) translateZ(20px); } .navs li:hover { transform: rotateX(-90deg); } </style> </head> <body> <div class="navs"> <ul> <li><a href="#">首頁</a><a href="#">Index</a></li> <li><a href="#">登入</a><a href="#">Login</a></li> <li><a href="#">註冊</a><a href="#">Register</a></li> </ul> </div> </body> </html> ``` ### 空間縮放 ![](https://i.imgur.com/nqQCbPV.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> ul { margin: 0; padding: 0; list-style: none; } .navs { width: 300px; height: 40px; margin: 50px auto; } .navs li { float: left; width: 100px; height: 40px; line-height: 40px; transition: all 0.5s; transform-style: preserve-3d; /* 觀察時使用 */ /* transform: rotateX(-20deg) rotateY(30deg); */ transform: scale3d(0.5, 1.5, 10); } .navs li a { position: absolute; left: 0; top: 0; display: block; width: 100%; height: 100%; text-align: center; text-decoration: none; color: white; } .navs li a:first-child { background-color: green; transform: translateZ(20px); } .navs li a:last-child { background-color: orange; transform: rotateX(90deg) translateZ(20px); } .navs li:hover { transform: rotateX(-90deg); } </style> </head> <body> <div class="navs"> <ul> <li><a href="#">首頁</a><a href="#">Index</a></li> <li><a href="#">登入</a><a href="#">Login</a></li> <li><a href="#">註冊</a><a href="#">Register</a></li> </ul> </div> </body> </html> ``` ## 動畫 ![](https://i.imgur.com/tBhzJYu.jpg) ![](https://i.imgur.com/DhcwLfX.jpg) ### 基本動畫 ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 200px; height: 100px; background-color: pink; animation: change 1s; } @keyframes change{ from{ width: 200px; } to{ width: 600px; } } </style> </head> <body> <div class="box"></div> </body> </html> ``` ### 百分比動畫 ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 200px; height: 100px; background-color: pink; animation: change 1s; } @keyframes change { 0% { width: 200px; } 50% { width: 500px; height: 300px; } 100% { width: 800px; height: 500px; } } </style> </head> <body> <div class="box"></div> </body> </html> ``` ### 動畫屬性 ![](https://i.imgur.com/QWtUPMD.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 200px; height: 100px; background-color: pink; animation: change 1s steps(3) 2s 3; } @keyframes change{ from{ width: 200px; } to{ width: 600px; } } </style> </head> <body> <div class="box"></div> </body> </html> ``` ``` animation: change 1s steps(3) 2s 3; change:動畫名稱 1s:動畫長度 step(3):斷點形動畫,3個斷點 2s:從無到開始的等待時間 3:重複次數 ``` ``` animation: change 1s infinite; infinite:無限循環 ``` ``` animation: change 1s backwards; backwards:結束狀態默認值 ``` ``` animation: change 1s forwards; forwards:動畫停留在結束狀態 ``` ### 複習複合屬性 ![](https://i.imgur.com/gsFtp4o.jpg) ![](https://i.imgur.com/RU7n7Xt.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 200px; height: 100px; background-color: pink; animation-name: change; animation-duration: 1s; animation-iteration-count: infinite; } .box:hover { animation-play-state: paused; } @keyframes change { from { width: 200px; } to { width: 600px; } } </style> </head> <body> <div class="box"></div> </body> </html> ``` ### 逐幀動畫 ![](https://i.imgur.com/9roJSvV.jpg) ```css= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 80px; height: 143px; /* border: 1px solid black; */ background-image: url(./p050image/anima.png); animation: move 1s steps(8) infinite, run 3s forwards; } @keyframes move { /* from { background-position: 0 0; } */ to { background-position: -640px 0; } } @keyframes run { /* from { transform: translateX(0); } */ to { transform: translateX(800px); } } </style> </head> <body> <div class="box"></div> </body> </html> ```