DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
用 CSS3 transform 打造可旋轉的 3D 立方體(HTML5 實踐)
編輯:HTML5詳解     

這一篇要示範如何純用 CSS3 做出一個 3D 立方體,並且用上下左右鍵控制它翻轉。

Demo 網址:https://www.mb5u.com/jiaoben/70022.html

HTML 結構

<div id="experiment">
  <div id="cube">
    <div class="face one">One face</div>
    <div class="face two">Up, down, left, right</div>
    <div class="face three">Lorem ipsum.</div>
    <div class="face four">New forms of navigation are fun.</div>
    <div class="face five">Rotating 3D cube</div>
    <div class="face six">More content</div>
  </div>
</div>

class 為 face 的六個 div 分別代表立方體的六個面,再用 one 到 six 區分。外層有 cube 與 experiment 兩層容器,兩層都是必要的,少任何一個 3D 效果都出不來。

其中 experiment 扮演的是「鏡頭」的角色。要對它設定焦距,3D 效果才能在內部元素上呈現出來。

perspective 屬性定義 z 軸方向的景深,同時也決定了遠近元素的相對大小。簡單記:perspective 值越小,物體看起來越大、離你越近;值越大,物體越小、離你越遠。可以參考 https://www.webkit.org/blog-files/3d-transforms/perspective-by-example.html 看實際對比。

perspective-origin 屬性則定義視角的原點位置。

場景(experiment)的 CSS

#experiment {
  -webkit-perspective: 800;
  -moz-perspective: 800;
  -o-perspective: 800;
  perspective: 800;

  -webkit-perspective-origin: 50% 200px;
  -moz-perspective-origin: 50% 200px;
  -o-perspective-origin: 50% 200px;
  perspective-origin: 50% 200px;
}

立方體(cube)的 CSS

#cube {
  position: relative;
  margin: 100px auto;
  height: 400px;
  width: 400px;

  -webkit-transition: -webkit-transform 2s linear;
  -moz-transition: -moz-transform 2s linear;
  -o-transition: -o-transform 2s linear;
  transition: transform 2s linear;

  -webkit-transform-style: preserve-3d;
  -moz-transform-style: preserve-3d;
  -o-transform-style: preserve-3d;
  transform-style: preserve-3d;
}

這裡有幾個重點。position: relative 加上固定的寬高是必要的,這樣子元素才會在限定範圍內進行 3D 位移;如果把寬高設成 100%,元素會在整個頁面範圍內跑。transition 用來設定動畫的時間與曲線。transform-style: preserve-3d 則讓 cube 內的所有元素作為一個整體來產生 3D 立體感——少了這行,子元素會被壓平。想深入了解可以參考 https://www.zhangxinxu.com/wordpress/2012/09/css3-3d-transform-perspective-animate-transition/ 。

六個面的共同樣式

.face {
  position: absolute;
  height: 360px;
  width: 360px;
  padding: 20px;
  background-color: rgba(50, 50, 50, 0.7);
}

每個面寬高 360 加上左右各 20 的 padding,合計正好 400,與 cube 的邊長一致。所有面都用 position: absolute 疊在同一個位置上,接下來才靠 transform 把它們推到各自的方位。

六個面的 3D 定位

接著設定每個面的空間方向:用 rotateX 或 rotateY 決定面朝哪邊,用 translateZ 把面推到三維空間中的正確位置。這裡的 200px 剛好是邊長 400 的一半。

#cube .one   { transform: rotateX(90deg)  translateZ(200px); }
#cube .two   { transform: translateZ(200px); }
#cube .three { transform: rotateY(90deg)  translateZ(200px); }
#cube .four  { transform: rotateY(180deg) translateZ(200px); }
#cube .five  { transform: rotateY(-90deg) translateZ(200px); }
#cube .six   { transform: rotateX(-90deg) translateZ(200px); }

(實作時每個宣告前面都還要加上 -webkit-、-moz-、-o- 前綴,以對應當時的瀏覽器版本。)

這裡有個順序上的細節值得注意:transform 是從右到左作用的,所以 rotateX(90deg) translateZ(200px) 的意思是「先沿 z 軸推出去 200,再整體繞 x 軸旋轉 90 度」,這樣六個面才會正確地圍成一個箱體。

加上鍵盤互動

最後為頁面綁定 keydown 事件,按下方向鍵時就改變旋轉角度,搭配前面設定的 transition 即可產生翻轉動畫。

var xAngle = 0, yAngle = 0;

document.addEventListener('keydown', function (e) {
  switch (e.keyCode) {
    case 37: // left
      yAngle -= 90;
      break;
    case 38: // up
      xAngle += 90;
      break;
    case 39: // right
      yAngle += 90;
      break;
    case 40: // down
      xAngle -= 90;
      break;
  }

  var cube = document.getElementById('cube');
  cube.style.transform = "rotateX(" + xAngle + "deg) rotateY(" + yAngle + "deg)";
  cube.style.webkitTransform = cube.style.transform;
}, false);

課程就到這裡,接下來動手試試:把 perspective 調小看看物體如何放大、換掉每個面的背景色,或把 translateZ 改成別的值觀察立方體如何被拉長變形,會對 3D transform 的運作方式更有感覺。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved