星級評分是內容站的常見需求,但真正做好看、做好用並不容易。本例會完成四件事:滑過星星有過渡動畫、分數即時更新、後端擋掉重複投票,並在前端給出提示。
<div class="rate">
<div class="big_rate">
<span rate="2"></span>
<span rate="4"></span>
<span rate="6"></span>
<span rate="8"></span>
<span rate="10"></span>
<div style="width:45px;" class="big_rate_up"></div>
</div>
<p><span id="s" class="s"></span><span id="g" class="g"></span></p>
<div id="my_rate"></div>
</div>
拆解一下:#big_rate 是灰色底星,#big_rate_up 是覆蓋在上面的亮星(用寬度控制顯示幾分之幾),#s 與 #g 分別顯示分數的整數與小數部分,#my_rate 放提示訊息。
.rate{width:600px; margin:100px auto; font-size:14px; position:relative; padding:10px 0;}
.rate p {margin:0; padding:0; display:inline; height:40px; overflow:hidden; position:absolute; top:0; left:100px; margin-left:140px;}
.rate p span.s {font-size:36px; line-height:36px; float:left; font-weight:bold; color:#DD5400;}
.rate p span.g {font-size:22px; display:block; float:left; color:#DD5400;}
.big_rate {width:140px; height:28px; text-align:left; position:absolute; top:3px; left:85px; display:inline-block; background:url(star.gif) left bottom repeat-x;}
.big_rate span {display:inline-block; width:24px; height:28px; position:relative; z-index:1000; cursor:pointer; overflow:hidden;}
.big_rate_up {width:140px; height:28px; position:absolute; top:0; left:0; background:url(star.gif) left top;}
#my_rate{ position:absolute; margin-top:40px; margin-left:100px}
#my_rate span{color:#dd5400; font-weight:bold}
寫一個 get_rate(rate) 函式負責前端呈現,參數就是平均分:
function get_rate(rate){
rate = rate.toString();
var s, g;
$("#g").show();
if (rate.length >= 3) { s = 10; g = 0; $("#g").hide(); } // 滿分
else if (rate == "0") { s = 0; g = 0; }
else { s = rate.substr(0, 1); g = rate.substr(1, 1); }
$("#s").text(s);
$("#g").text("." + g);
}
接著讓亮星層依分數做動畫伸展:
$(".big_rate_up").animate({ width: (parseInt(s) + parseInt(g) / 10) * 14, height: 26 }, 1000);
$(".big_rate span").each(function(){
$(this).mouseover(function(){
$(".big_rate_up").width($(this).attr("rate") * 14);
$("#s").text($(this).attr("rate"));
$("#g").text("");
}).click(function(){
// 送出評分,見下方 ajax
});
});
為什麼要乘以 14?因為一張星星圖寬 28px,五張代表滿分 10 分,單一分值佔 (5 × 28) / 10 = 14 px。
var score = $(this).attr("rate");
$("#my_rate").html("您的評分:<span>" + score + "</span>");
$.ajax({
type: "POST",
url: "post.php",
data: "score=" + score,
success: function (msg) {
if (msg == 1) { $("#my_rate").html("<span>您已經評過分了!</span>"); }
else if (msg == 2) { $("#my_rate").html("<span>您評過分了!</span>"); }
else { get_rate(msg); } // 後端回傳新的平均分
}
});
別忘了滑鼠移開時要把分數還原成平均值:
$(".big_rate").mouseout(function(){
$("#s").text(s);
$("#g").text("." + g);
$(".big_rate_up").width((parseInt(s) + parseInt(g) / 10) * 14);
});
函式寫好後,頁面載入時呼叫一次即可:
$(function(){ get_rate(8.8); });
post.php 要接收分數,並用 cookie 記錄投票者 IP 與時間,避免同一個人反覆灌票。
include_once ('connect.php');
$score = $_POST['score'];
if (isset($score)) {
$cookiestr = getip();
$time = time();
if (isset($_COOKIE['person']) && $_COOKIE['person'] == $cookiestr) {
echo "1"; // 這個 IP 投過了
} elseif (isset($_COOKIE['rate_time']) && ($time - intval($_COOKIE['rate_time'])) < 600) {
echo "2"; // 冷卻時間未到
} else {
mysql_query("update raty set voter=voter+1, total=total+'$score' where id=1");
$query = mysql_query("select * from raty where id=1");
$rs = mysql_fetch_array($query);
$aver = round($rs['total'] / $rs['voter'], 1) * 10;
setcookie("person", $cookiestr, time() + 3600);
setcookie("rate_time", time(), time() + 3600);
echo $aver; // 回傳新平均分
}
}
第一次投票時寫入分數並回傳新的平均值;重複投票則回傳提示碼,由前端顯示對應文案。
CREATE TABLE IF NOT EXISTS `raty` ( `id` int(11) NOT NULL auto_increment, `voter` int(10) NOT NULL default '0' COMMENT '評分次數', `total` int(11) NOT NULL default '0' COMMENT '總分', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
整套流程就是:前端畫星星 → 使用者點擊 → Ajax 送分數 → PHP 落庫並回傳平均 → 前端重繪。理解之後,替換成自己的圖片與欄位名稱就能直接用在專案裡。