DIV CSS 佈局教程網

用原生 JS 做星星評分:滑過預覽、點擊定案
編輯:JavaScript綜合知識     

星星評分有三個行為要同時處理:滑過時連續點亮並顯示文字說明、滑走時全部恢復、點擊後記錄結果。它的實作難度不高,但狀態管理的順序需要想清楚。

效果規格

  1. 游標移入時星星點亮,下方顯示對應文字;移出時恢復灰色,文字隱藏。
  2. 移到第 N 顆時,前 N 顆都要亮。
  3. 點擊後把分數記錄下來。

樣式

.wrapper { width:300px; margin:10px auto; font:14px/1.5 arial; }
#star { overflow:hidden; }
#star li { float:left; width:20px; height:20px; margin:2px; display:inline;
           color:#999; font:bold 18px arial; cursor:pointer; }
#star .act { color:#c00; }
#star_word { width:80px; height:30px; line-height:30px; border:1px solid #ccc;
             margin:10px; text-align:center; display:none; }

用文字星號 ★ 並以 color 切換深淺,是最省事的做法,不必準備圖片。

結構

<div class="wrapper">
  打分結果 <span id="result"></span>
  <ul id="star">
    <li>★</li><li>★</li><li>★</li><li>★</li><li>★</li>
  </ul>
  <div id="star_word">一般</div>
</div>

程式

window.onload = function () {
  var star = document.getElementById("star");
  var star_li = star.getElementsByTagName("li");
  var star_word = document.getElementById("star_word");
  var result = document.getElementById("result");
  var len = star_li.length;
  var word = ['很差', '差', '一般', '好', '很好'];

  for (var i = 0; i < len; i++) {
    star_li[i].index = i;

    star_li[i].onmouseover = function () {
      star_word.style.display = "block";
      star_word.innerHTML = word[this.index];
      for (var j = 0; j <= this.index; j++) { star_li[j].className = "act"; }
    };

    star_li[i].onmouseout = function () {
      star_word.style.display = "none";
      for (var j = 0; j < len; j++) { star_li[j].className = ""; }
    };

    star_li[i].onclick = function () {
      result.innerHTML = (this.index + 1) + "分";
    };
  }
};

兩個關鍵:j <= this.index 是「連通前 N 顆」的寫法;this.index + 1 是因為 index 從 0 起算,而分數要從 1 起算。

三個必須修掉的問題

點擊的分數被 mouseout 清掉了。原版滑走後雖然 result 保留分數,但星星全變灰,視覺上與「未評分」無法區分。正確做法是記住已選分數,滑出時重現它:

var score = 0;

function paint(n) {
  for (var j = 0; j < len; j++) star_li[j].className = j <= n ? "act" : "";
}

star_li[i].onmouseover = function () { paint(this.index); star_word.style.display = "block"; };
star_li[i].onmouseout  = function () { paint(score - 1);  star_word.style.display = "none"; };
star_li[i].onclick     = function () { score = this.index + 1; paint(score - 1);
                                       result.textContent = score + "分"; };

迴圈變數共用。三個事件處理器裡都用外層的 i/j,改用 let 或另取變數名較安全。

無法用鍵盤操作。這是最嚴重的問題。滑鼠專用的評分元件把一部分使用者排除在外。改用 radio 按鈕實作:

<fieldset>
  <legend>請為服務打分</legend>
  <input type="radio" name="score" id="s1" value="1"><label for="s1">★ 很差</label>
  <input type="radio" name="score" id="s2" value="2"><label for="s2">★ 差</label>
  <input type="radio" name="score" id="s3" value="3"><label for="s3">★ 一般</label>
  <input type="radio" name="score" id="s4" value="4"><label for="s4">★ 好</label>
  <input type="radio" name="score" id="s5" value="5"><label for="s5">★ 很好</label>
</fieldset>

radio 天生支援方向鍵切換、Tab 聚焦,選取值也能直接隨表單送出。搭配 flex-direction: row-reverse 與 input:checked ~ label 選擇器,連 JS 都可以省下來。文字標籤也別省略——顏色深淺對色盲使用者是無效的資訊。

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