DIV CSS 佈局教程網

JS 跑馬燈抽獎動畫:轉動隨機數並對獎的完整實作
編輯:關於JavaScript     

節假日常見的抽獎遊戲,背後其實就是一個跑馬燈:讓高亮框沿著格子依序前進,先加速、跑幾圈、再減速停在預先決定的位置。這個範例用 14 個格子圍成一圈,加上 jQuery 操作 class,就能做出完整的抽獎體驗。

版面與樣式

<style type="text/css">
.box { padding:9px 0 0 11px; width:759px; height:611px; background:#ccc; margin:20px auto; }
.inbox { width:751px; height:596px; position:relative; }
#random_box li { position:absolute; width:144px; height:144px; overflow:hidden;
                 border:3px solid #c7c5ca; background:#fff; }
.ok { display:block; width:442px; height:294px; background:#c00; color:#fff;
      font-size:48px; position:absolute; top:149px; left:148px;
      cursor:pointer; text-align:center; line-height:280px; }
#random_1 { left:0;   top:0; }     #random_2 { left:148px; top:0; }
#random_3 { left:296px; top:0; }   #random_4 { left:444px; top:0; }
#random_5 { left:592px; top:0; }   #random_6 { left:592px; top:148px; }
#random_7 { left:592px; top:298px; } #random_8 { left:592px; top:446px; }
#random_9 { left:444px; top:446px; } #random_10 { left:296px; top:446px; }
#random_11 { left:148px; top:446px; } #random_12 { left:0; top:446px; }
#random_13 { left:0; top:298px; }  #random_14 { left:0; top:148px; }
#random_box li.random_current { border:1px solid red; }
</style>

核心邏輯

程式的關鍵變數有幾個:index 是目前亮格、cycle 是已跑圈數、EndCycle 是預定圈數、EndIndex 是「剩幾格時開始減速」、random_num 是預先抽出的中獎格。減速點的計算方式是把中獎位置往前推 5 格,這樣跑完最後 5 格剛好落在得獎位置。

<script src="https://code.jquery.com/jquery-1.7.1.js"></script>
<script>
var index = 1,        // 當前亮區位置
    arr_length = 14,  // 格子總數
    Speed = 300,      // 初始速度(毫秒)
    Time,
    EndIndex = 1,     // 決定在哪一格開始變慢
    cycle = 0,        // 已轉動圈數
    EndCycle = 3,     // 目標圈數
    flag = false,     // 是否進入減速階段
    random_num = 1,   // 中獎格
    quick = 0;        // 加速計數

function StartGame() {
  $("#random_box li").removeClass("random_current");
  // 實務上這裡應由伺服器回傳中獎結果
  random_num = Math.floor(Math.random() * 12) + 2;   // 2 ~ 13

  index = 1;
  cycle = 0;
  flag = false;

  if (random_num > 5) {
    EndIndex = random_num - 5;         // 前 5 格開始變慢
  } else {
    EndIndex = random_num + 14 - 5;
  }
  Time = setInterval(Star, Speed);
}

function Star() {
  if (flag == false) {
    // 走滿 5 格後加速
    if (quick == 5) {
      clearInterval(Time);
      Speed = 50;
      Time = setInterval(Star, Speed);
    }
    // 跑滿目標圈數且到達減速點
    if (cycle == EndCycle + 1 && index - 1 == EndIndex) {
      clearInterval(Time);
      Speed = 300;
      flag = true;
      Time = setInterval(Star, Speed);
    }
  }

  if (index > arr_length) { index = 1; cycle++; }

  // 停在得獎格
  if (flag == true && index == parseInt(random_num, 10)) {
    quick = 0;
    clearInterval(Time);
  }

  $("#random_" + index).addClass('random_current');
  var prevIndex = index > 1 ? index - 1 : arr_length;
  $("#random_" + prevIndex).removeClass('random_current');
  index++;
  quick++;
}
</script>

HTML 部分就是 14 個帶 id 為 random_1 到 random_14 的 li,加上一個 onclick 觸發 StartGame() 的中獎按鈕。

可以再加強的地方

視覺上可以把每格換成圖片、加上音效與彩帶動畫;節奏上手動微調 Speed 與減速格數,會明顯影響「緊張感」。但有一點必須特別注意:中獎結果絕對不能由前端決定。上面用 Math.random() 只是為了示範動畫,真實活動一定要由伺服器產生結果並記錄,前端只負責把動畫停在伺服器給的位置。否則使用者打開開發者工具就能改變結果,活動的可信度與公平性都蕩然無存。

另外記得在動畫進行中鎖定按鈕避免重複點擊,並在頁面卸載前清掉計時器,以免殘留的 setInterval 繼續跑。

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