上網易前端微專業課程,裡面有一個課外作業是實現一個3D旋轉立方體,花了點時間做了下,還有點意思,寫個簡單教程,供大家學習。 先放上最終要實現的效果 .
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>CSS3 3D立方體多邊形動畫特效 - 代碼筆記</title><style type="text/css">*{ margin:0 auto; padding:0;}@keyframes rotate{ 0%{ transform:rotateX(0deg) rotateY(0deg); } 100%{ transform:rotateX(360deg) rotateY(360deg); }}html{ background:linear-gradient(#ff0 0%, #000 80%); height:100%;}.wrap{ margin-top:200px; perspective: 1000px; /* 視圖距元素的距離 相當於攝像機 */}.cube{ width:200px; height:200px; position:relative; color:#fff; font-size:36px; font-weight:bold; text-align:center; line-height:200px; transform-style:preserve-3d; /* 預設flat 2D */ transform:rotateX(-30deg) rotateY(-70deg); animation:rotate 20s infinite linear; /*播放時間 播放次數為迴圈 緩動效果為勻速 */}.cube > div{ width:100%; height:100%; border:1px solid #fff; position:absolute; background-color:#333; opacity:.6; transition:transform 0.4s ease-in;}.cube .out-front{ transform: translateZ(100px);}.cube .out-back{ transform: translateZ(-100px) rotateY(180deg);}.cube .out-left{ transform: translateX(-100px) rotateY(-90deg);}.cube .out-right{ transform: translateX(100px) rotateY(90deg);}.cube .out-top{ transform: translateY(-100px) rotateX(90deg);}.cube .out-bottom{ transform: translateY(100px) rotateX(-90deg);}.cube > span{ display:block; width:100px; height:100px; border:1px solid black; background-color:#999; position:absolute; top:50px; left:50px;}.cube .in-front{ transform: translateZ(50px);}.cube .in-back{ transform: translateZ(-50px) rotateY(180deg);}.cube .in-left{ transform: translateX(-50px) rotateY(-90deg);}.cube .in-right{ transform: translateX(50px) rotateY(90deg);}.cube .in-top{ transform: translateY(-50px) rotateX(90deg);}.cube .in-bottom{ transform: translateY(50px) rotateX(-90deg);}.wrap:hover .out-front{ transform: translateZ(200px);}.wrap:hover .out-back{ transform: translateZ(-200px) rotateY(180deg);}.wrap:hover .out-left{ transform: translateX(-200px) rotateY(-90deg);}.wrap:hover .out-right{ transform: translateX(200px) rotateY(90deg);}.wrap:hover .out-top{ transform: translateY(-200px) rotateX(90deg);}.wrap:hover .out-bottom{ transform: translateY(200px) rotateX(-90deg);} </style></head><body><div class="wrap"> <div class="cube"> <div class="out-front">前面</div> <div class="out-back">後面</div> <div class="out-left">左面</div> <div class="out-right">右面</div> <div class="out-top">上面</div> <div class="out-bottom">下面</div> <span class="in-front"></span> <span class="in-back"></span> <span class="in-left"></span> <span class="in-right"></span> <span class="in-top"></span> <span class="in-bottom"></span> </div></div> <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';"><p>適用瀏覽器:360、FireFox、Chrome、Opera、傲遊、搜狗、世界之窗. 不支援Safari、IE8及以下瀏覽器。</p></div></body></html>
【相關推薦】
1. 精選:“php程式員工具箱”V0.1版本下載
2. 免費css線上視頻教程
3. php.cn獨孤九賤(2)-css視頻教程