DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS3 動畫實作:讓齒輪真正轉起來
編輯:CSS詳解     

這個範例示範用 CSS3 的 transform 與 animation 做出一組轉動的齒輪。整個圖形完全沒有圖片:齒輪本體是一個圓形,十個小方塊絕對定位後各自旋轉不同角度構成齒牙,再用 @keyframes 讓它持續自轉。

基礎樣式與齒輪本體

body { background:#2C91AE; }

.wrap {
  width:600px; height:400px;
  padding:50px; margin:20px auto;
  position:relative;
}

.gear {
  border-radius:100px;
  border:1px solid #8C8C8A;
  width:200px; height:200px;
  position:relative;
  background-color:#FFFFFF;
}

/* 每一個 span 就是一根齒牙 */
.gear span {
  border:1px solid #8C8C8A;
  display:block;
  width:20px; height:30px;
  position:absolute;
  border-right:none;
  background-color:#FFFFFF;
}

十根齒牙的定位與旋轉

.c1  { left:8px;   top:12px;  -webkit-transform:rotate(41deg); }
.c2  { left:60px;  top:-21px; -webkit-transform:rotate(75deg); }
.c3  { left:125px; top:-19px; -webkit-transform:rotate(110deg); }
.c4  { left:177px; top:20px;  -webkit-transform:rotate(144deg); }
.c5  { left:198px; top:84px;  -webkit-transform:rotate(181deg); }
.c6  { left:176px; top:150px; -webkit-transform:rotate(216deg); }
.c7  { left:125px; top:187px; -webkit-transform:rotate(250deg); }
.c8  { left:55px;  top:188px; -webkit-transform:rotate(288deg); }
.c9  { left:0px;   top:147px; -webkit-transform:rotate(325deg); }
.c10 { left:-19px; top:80px;  -webkit-transform:rotate(2deg); }

.gear em {
  background-color:#2C91AE;
  border-radius:20px;
  border:1px solid #8C8C8A;
  width:40px; height:40px;
  display:block;
  position:absolute;
  left:80px; top:80px;
}

齒牙的角度間隔約 36 度(360 ÷ 10),位置則沿著圓周分布——這組數字是手調出來的,改動齒數時必須同步重算座標與角度。

旋轉動畫

.gear {
  -webkit-animation-name: run;
  -webkit-animation-duration: 5s;
  -webkit-animation-timing-function: linear;
  -webkit-animation-delay: 0s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-direction: normal;
}

.s {
  position:absolute;
  left:700px; top:156px;
  zoom:0.7;
  -webkit-animation-duration:3s;
}

@-webkit-keyframes run {
  from { -webkit-transform:rotate(0deg); }
  to   { -webkit-transform:rotate(360deg); }
}

兩個齒輪共用同一組 keyframes,但小齒輪的 duration 設為 3s、大齒輪 5s,轉速因此不同——這正是真實齒輪嚙合時的視覺特徵。timing-function 必須是 linear,如果用 ease-in-out,轉速會忽快忽慢,看起來就不像機械了。

外框管線

.line {
  border-width:3px 0 3px 3px;
  border-color:#FFFFFF;
  border-style:solid;
  width:117px; height:234px;
  border-radius:120px 0 0 120px;
  position:absolute;
  top:31px; left:30px;
}
.line div { position:relative; border-color:#FFFFFF; border-style:solid; }
.la {
  border-width:3px 0 0;
  top:30px; left:112px; width:432px;
  -webkit-transform:rotate(9deg);
}
.lb {
  border-width:3px 0 0;
  top:227px; left:110px; width:420px;
  -webkit-transform:rotate(-1deg);
}
.lc {
  border-width:3px 3px 3px 0;
  width:75px; height:160px;
  border-radius:0 79px 79px 0;
  left:531px; top:61px;
  -webkit-transform:rotate(5deg);
}

HTML 結構

<div class="wrap">
  <div class="gear">
    <span class="c1"></span>
    <span class="c2"></span>
    <!-- c3 ~ c10 略 -->
    <em></em>
  </div>

  <div class="gear s">
    <span class="c1"></span>
    <span class="c2"></span>
    <!-- c3 ~ c10 略 -->
    <em></em>
  </div>

  <div class="line">
    <div class="la"></div>
    <div class="lb"></div>
    <div class="lc"></div>
  </div>
</div>

這份程式碼是以 -webkit- 前綴寫成的,在標準瀏覽器上建議補上無前綴的 transform 與 animation 版本,並加上 @-keyframes 的標準寫法,才能確保跨瀏覽器一致。

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