JavaScript制作淘寶星級評分效果的思路
小編也是剛開始學(xué)JavaScript,覺得淘寶評星效果很棒,于是產(chǎn)生了自己寫一個的想法,先給大家分享一下實現(xiàn)效果:

現(xiàn)附上自己寫的源代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script language="JavaScript" type="text/javascript">
function star(n)
{
var array=new Array();
array[0]=document.getElementById("oneStar");
array[1]=document.getElementById("twoStar");
array[2]=document.getElementById("threeStar");
array[3]=document.getElementById("fourStar");
array[4]=document.getElementById("fiveStar");
for(var i=0;i<=n;i++)
{
array[i].innerText="★";
}
for( var j=4;j>n;j--)
{
array[j].innerText="☆";
}
document.getElementById("evaluate").innerText="您的評價是"+(n+1)+"星";
}
</script>
<title>評星</title>
</head>
<body>
<strong>請您對我們作出評價:</strong>
<span id="star">
<span style="cursor: pointer " onclick="star(0)"id="oneStar" >☆</span>
<span style="cursor: pointer " onclick="star(1)" id="twoStar" >☆</span>
<span style="cursor: pointer " onclick="star(2)" id="threeStar" >☆</span>
<span style="cursor: pointer " onclick="star(3)" id="fourStar" >☆</span>
<span style="cursor: pointer " onclick="star(4)" id="fiveStar" >☆</span>
</span><span id="evaluate"></span>
</body>
</html>
一開始的時候用了兩個for循環(huán)就是這樣的:
for(var i=0;i<=n;i++)
{
document.getElementById("fiveStar").innerText="★";
}
for( var j=4;j>n;j--)
{
document.getElementById("fiveStar").innerText="☆";
}
大神們估計已經(jīng)看出來了,在for循環(huán)之后HTML里的span已經(jīng)失去了作用,也就是說它只能評價一次.....
于是順著這個思路想到了用數(shù)組解決這個問題,就是讓評星效果里的每一顆星儲存到數(shù)組里,寫出了上述的代碼,可樓主還犯了一個小錯誤,著實困惱了許久....
array[0]=document.getElementById("oneStar").innerText;
通過這樣定義的數(shù)組....結(jié)果可想而知,后面的代碼根本無法改變評星,后來意識到,這樣的定義直接將ID為onestar的元素的內(nèi)容賦值給了數(shù)組,也就是說數(shù)組成了一個指向數(shù)組的指針....自然無法改變對應(yīng)元素的值.后來總算明白了....
之后又加了一些CSS效果
成品是這樣的:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>淘寶評分效果</title>
<style type="text/css">
ul, li {margin: 0; padding: 0; border: 0;}
.shop-rating {
height: 25px;
overflow: hidden;
zoom: 1;
padding: 2px 0;
position: relative;
z-index: 999;
font:12px Arial;
color:#000;
line-height:1.2em
}
.shop-rating span {
height: 23px;
display: block;
line-height: 23px;
float: left;
}
.shop-rating span.title {
width: 125px;
text-align: right;
margin-right: 5px;
}
.shop-rating ul {
float: left;
}
.shop-rating .result {
margin-left: 20px;
padding-top: 2px;
}
.shop-rating .result span {
color: #ff6d02;
}
.rating-level,
.rating-level a {
background: url(//img.jbzj.com/demoimg/201007/o_star.png) no-repeat scroll 1000px 1000px;
}
.rating-level {
background-position: 0px 0px;
width: 120px;
height: 23px;
position: relative;
z-index: 1000;
}
.shop-rating .result em {
color: #f60;
font-family: arial;
font-weight: bold;
}
.rating-level li {
display: inline;
}
.rating-level a {
line-height: 23px;
height: 23px;
position: absolute;
top: 0px;
left: 0px;
text-indent: -999em;
*zoom: 1;
outline: none;
}
.rating-level a.one-star {
width: 20%;
z-index: 6;
}
.rating-level a.two-stars {
width: 40%;
z-index: 5;
}
.rating-level a.three-stars {
width: 60%;
z-index: 4;
}
.rating-level a.four-stars {
width: 80%;
z-index: 3;
}
.rating-level a.five-stars {
width: 100%;
z-index: 2;
}
.rating-level .current-rating, .rating-level a:hover {background-position:0 -28px}
.rating-level a.one-star:hover,.rating-level a.two-stars:hover,.rating-level a.one-star.current-rating,.rating-level a.two-stars.current-rating{background-position:0 -116px;}
.rating-level .three-stars .current-rating,.rating-level .four-stars .current-rating,.rating-level .five-stars .current-rating{background-position:0 -28px;}
</style>
</head>
<body>
<div class="shop-rating">
<span class="title">你對我人品的評價:</span>
<ul class="rating-level" id="stars2">
<li><a href="javascript:void(0);" class="one-star" star:value="20">20</a></li>
<li><a href="javascript:void(0);" class="two-stars" star:value="40">40</a></li>
<li><a href="javascript:void(0);" class="three-stars" star:value="60">60</a></li>
<li><a href="javascript:void(0);" class="four-stars" star:value="80">80</a></li>
<li><a href="javascript:void(0);" class="five-stars" star:value="100">100</a></li>
</ul>
<span id="stars2-tips" class="result"></span>
<input type="hidden" id="stars2-input" name="b" value="" size="2" />
</div>
<script>
var TB = function() {
var T$ = function(id) { return document.getElementById(id) }
var T$$ = function(r, t) { return (r || document).getElementsByTagName(t) }
var Stars = function(cid, rid, hid, config) {
var lis = T$$(T$(cid), 'li'), curA;
for (var i = 0, len = lis.length; i < len; i++) {
lis[i]._val = i;
lis[i].onclick = function() {
T$(rid).innerHTML = '<em>' + (T$(hid).value = T$$(this, 'a')[0].getAttribute('star:value')) + '分</em> - ' + config.info[this._val];
curA = T$$(T$(cid), 'a')[T$(hid).value / config.step - 1];
};
lis[i].onmouseout = function() {
curA && (curA.className += config.curcss);
}
lis[i].onmouseover = function() {
curA && (curA.className = curA.className.replace(config.curcss, ''));
}
}
};
return {Stars: Stars}
}().Stars('stars2', 'stars2-tips', 'stars2-input', {
'info' : ['人品極差', '人品不咋地', '人品一般吧', '人品不錯', '人品極好啊'],
'curcss': ' current-rating',
'step': 20
});
</script>
</body>
</html>
以上就是JavaScript制作淘寶星級評分效果的思路,語言很直白,易理解,希望對大家的學(xué)習(xí)有所幫助,和小編一起去探索javascript更多的神奇之處,共同進(jìn)步。
相關(guān)文章
window.location和document.location的區(qū)別分析
用戶不能改變document.location(因為這是當(dāng)前顯示文檔的位置)。但是,可以改變window.location (用其它文檔取代當(dāng)前文檔)window.location本身也是一個對象,而document.location不是對象2008-12-12
利用JavaScript實現(xiàn)春節(jié)倒計時效果(移動端和PC端)
這篇文章主要介紹了通過Html+Css+js如何實現(xiàn)春節(jié)倒計時效果,本文同時介紹了移動端和PC端兩種效果,感興趣的同學(xué)可以跟隨小編一起動手試試2022-01-01

