Javascript制作的评星打分系统
前不久用JQuery做了个评星打分的小东东,这次用javascript重写一遍。JQuery在浏览器事件兼容判断方面为我们做了很多底层工作,的确是大大的方便开发和减少的很多代码量。不过Javascript毕竟是最基本的东西,还是不能忘记噢。JQuery版的地址在这里,大家可以对比着看看。
演示效果如下:
Javascript代码:
1// JavaScript Document
2var descriptionTxt;
3var description = new Array("非常差,回去再练练","真的是差,都不忍心说你了","一般,还过得去吧","很好,是我想要的东西","太完美了,此物只得天上有,人间哪得几回闻!");
4function stopDefault(e){
5 if(e && e.preventDefault)
6 e.preventDefault();
7 else
8 window.event.returnValue = false;
9 return false;
10};
11
12var $ = function(id){
13 return typeof id == "string"? document.getElementById(id):id;
14}
15
16function addEventHandler(oTarget, sEventType, fnHandler){
17 if(oTarget.addEventHandler){
18 oTarget.addEventListener(sEventType,fnHandler,false);
19 }else if(oTarget.attachEvent){
20 oTarget.attachEvent("on"+sEventType,fnHandler);
21 }else{
22 oTarget["on"+sEventType] = fnHandler;
23 }
24};
25
26function changeStars(){
27 var starSys = $("xzw_starBox");
28 var stars = starSys.getElementsByTagName("a");
29 if(stars){
30 for(var i=0; i<stars.length; i++){
31 var star = stars[i];
32 addEventHandler(star,"mouseover",displayDes);
33 addEventHandler(star,"mouseout",displayDes);
34 addEventHandler(star,"click",showDes);
35 addEventHandler(starSys,"mouseover",changZindex);
36 addEventHandler(starSys,"mouseout",changZindex);
37 }
38 }
39}
40
41function changZindex(e){
42 var e = e || window.event;
43 if(e.type == 'mouseover'){
44 $("xzw_star").style.zIndex = "3";
45 }else if(e.type == 'mouseout'){
46 $("xzw_star").style.zIndex = "1";
47 }
48}
49
50function showDes(e){
51 var stepW = 26;
52 var e = e || window.event;
53 var titleTxt = null;
54 if(e.srcElement){
55 titleTxt = e.srcElement.title;
56 }else if(e.target){
57 titleTxt = e.target.title;
58 }
59 descriptionTxt = description[titleTxt-1];
60 $("description").innerHTML = descriptionTxt;
61 $("showb").style.width = stepW * titleTxt + "px";
62 return stopDefault(e);
63 return descriptionTxt;
64}
65
66function displayDes(e){
67 var e = e || window.event;
68 var titleTxt = null;
69 var descriptionTxtTemp = descriptionTxt;
70 if(e.srcElement){
71 titleTxt = e.srcElement.title
72 }else if(e.target){
73 titleTxt = e.target.title
74 }
75 var descriptionTxt2 = description[titleTxt-1];
76 if(e.type == 'mouseover'){
77 $("description").innerHTML = descriptionTxt2;
78 }else if(e.type == 'mouseout'){
79 if(descriptionTxtTemp != null){
80 $("description").innerHTML = descriptionTxtTemp;
81 }else{
82 $("description").innerHTML = "";
83 }
84 }
85 return stopDefault(e);
86}
87window.onload = changeStars;
2var descriptionTxt;
3var description = new Array("非常差,回去再练练","真的是差,都不忍心说你了","一般,还过得去吧","很好,是我想要的东西","太完美了,此物只得天上有,人间哪得几回闻!");
4function stopDefault(e){
5 if(e && e.preventDefault)
6 e.preventDefault();
7 else
8 window.event.returnValue = false;
9 return false;
10};
11
12var $ = function(id){
13 return typeof id == "string"? document.getElementById(id):id;
14}
15
16function addEventHandler(oTarget, sEventType, fnHandler){
17 if(oTarget.addEventHandler){
18 oTarget.addEventListener(sEventType,fnHandler,false);
19 }else if(oTarget.attachEvent){
20 oTarget.attachEvent("on"+sEventType,fnHandler);
21 }else{
22 oTarget["on"+sEventType] = fnHandler;
23 }
24};
25
26function changeStars(){
27 var starSys = $("xzw_starBox");
28 var stars = starSys.getElementsByTagName("a");
29 if(stars){
30 for(var i=0; i<stars.length; i++){
31 var star = stars[i];
32 addEventHandler(star,"mouseover",displayDes);
33 addEventHandler(star,"mouseout",displayDes);
34 addEventHandler(star,"click",showDes);
35 addEventHandler(starSys,"mouseover",changZindex);
36 addEventHandler(starSys,"mouseout",changZindex);
37 }
38 }
39}
40
41function changZindex(e){
42 var e = e || window.event;
43 if(e.type == 'mouseover'){
44 $("xzw_star").style.zIndex = "3";
45 }else if(e.type == 'mouseout'){
46 $("xzw_star").style.zIndex = "1";
47 }
48}
49
50function showDes(e){
51 var stepW = 26;
52 var e = e || window.event;
53 var titleTxt = null;
54 if(e.srcElement){
55 titleTxt = e.srcElement.title;
56 }else if(e.target){
57 titleTxt = e.target.title;
58 }
59 descriptionTxt = description[titleTxt-1];
60 $("description").innerHTML = descriptionTxt;
61 $("showb").style.width = stepW * titleTxt + "px";
62 return stopDefault(e);
63 return descriptionTxt;
64}
65
66function displayDes(e){
67 var e = e || window.event;
68 var titleTxt = null;
69 var descriptionTxtTemp = descriptionTxt;
70 if(e.srcElement){
71 titleTxt = e.srcElement.title
72 }else if(e.target){
73 titleTxt = e.target.title
74 }
75 var descriptionTxt2 = description[titleTxt-1];
76 if(e.type == 'mouseover'){
77 $("description").innerHTML = descriptionTxt2;
78 }else if(e.type == 'mouseout'){
79 if(descriptionTxtTemp != null){
80 $("description").innerHTML = descriptionTxtTemp;
81 }else{
82 $("description").innerHTML = "";
83 }
84 }
85 return stopDefault(e);
86}
87window.onload = changeStars;
HTML结构:
1<div id="xzw_starSys">
2 <div id="xzw_starBox">
3 <ul class="star" id="xzw_star">
4 <li><a href="#" title="1" class="one-star" hidefocus="true">1</a></li>
5 <li><a href="#" title="2" class="two-stars" hidefocus="true">2</a></li>
6 <li><a href="#" title="3" class="three-stars" hidefocus="true">3</a></li>
7 <li><a href="#" title="4" class="four-stars" hidefocus="true">4</a></li>
8 <li><a href="#" title="5" class="five-stars" hidefocus="true">5</a></li>
9 </ul>
10 <div class="current-rating" id="showb"></div>
11 </div>
12 <span id="description" class="description"></span>
13</div>
2 <div id="xzw_starBox">
3 <ul class="star" id="xzw_star">
4 <li><a href="#" title="1" class="one-star" hidefocus="true">1</a></li>
5 <li><a href="#" title="2" class="two-stars" hidefocus="true">2</a></li>
6 <li><a href="#" title="3" class="three-stars" hidefocus="true">3</a></li>
7 <li><a href="#" title="4" class="four-stars" hidefocus="true">4</a></li>
8 <li><a href="#" title="5" class="five-stars" hidefocus="true">5</a></li>
9 </ul>
10 <div class="current-rating" id="showb"></div>
11 </div>
12 <span id="description" class="description"></span>
13</div>
CSS代码:
1#xzw_starSys *{margin:0; padding:0}
2#xzw_starSys ul, #xzw_starSys li { list-style-type:none}
3#xzw_starSys{height:20px; width:500px; border:1px solid #cecece; margin:10px auto; padding:5px; font-size:12px; line-height:22px; overflow:hidden}
4#xzw_starBox{position:relative; width:130px; float:left; }
5#xzw_starSys .description {float:left; margin-left:20px}
6#xzw_starSys .star { height:20px; width:130px; position:relative; background:url(images/star.gif) left -40px repeat-x; cursor:pointer}
7#xzw_starSys .star li { float:left; padding:0px; margin:0px}
8#xzw_starSys .star li a { display:block; width:26px; height:20px; overflow:hidden; text-indent:-9999px; position:absolute; z-index:5; }
9#xzw_starSys .star li a:hover {background:url(images/star.gif) 0 -20px ; z-index:3; left:0; top:0 }
10#xzw_starSys .star li a:focus {outline:none}
11#xzw_starSys .star a.one-star {left:0; }
12#xzw_starSys .star a.one-star:hover {width:26px;}
13#xzw_starSys .star a.two-stars {left:26px; }
14#xzw_starSys .star a.two-stars:hover {width:52px;}
15#xzw_starSys .star a.three-stars {left:52px; }
16#xzw_starSys .star a.three-stars:hover {width:78px;}
17#xzw_starSys .star a.four-stars {left:78px; }
18#xzw_starSys .star a.four-stars:hover {width:104px;}
19#xzw_starSys .star a.five-stars {left:104px; }
20#xzw_starSys .star a.five-stars:hover {width:130px;}
21#xzw_starSys .current-rating{ background: url(images/star.gif) left top; position: absolute; height: 20px; z-index: 1; top:0; left:0 }
2#xzw_starSys ul, #xzw_starSys li { list-style-type:none}
3#xzw_starSys{height:20px; width:500px; border:1px solid #cecece; margin:10px auto; padding:5px; font-size:12px; line-height:22px; overflow:hidden}
4#xzw_starBox{position:relative; width:130px; float:left; }
5#xzw_starSys .description {float:left; margin-left:20px}
6#xzw_starSys .star { height:20px; width:130px; position:relative; background:url(images/star.gif) left -40px repeat-x; cursor:pointer}
7#xzw_starSys .star li { float:left; padding:0px; margin:0px}
8#xzw_starSys .star li a { display:block; width:26px; height:20px; overflow:hidden; text-indent:-9999px; position:absolute; z-index:5; }
9#xzw_starSys .star li a:hover {background:url(images/star.gif) 0 -20px ; z-index:3; left:0; top:0 }
10#xzw_starSys .star li a:focus {outline:none}
11#xzw_starSys .star a.one-star {left:0; }
12#xzw_starSys .star a.one-star:hover {width:26px;}
13#xzw_starSys .star a.two-stars {left:26px; }
14#xzw_starSys .star a.two-stars:hover {width:52px;}
15#xzw_starSys .star a.three-stars {left:52px; }
16#xzw_starSys .star a.three-stars:hover {width:78px;}
17#xzw_starSys .star a.four-stars {left:78px; }
18#xzw_starSys .star a.four-stars:hover {width:104px;}
19#xzw_starSys .star a.five-stars {left:104px; }
20#xzw_starSys .star a.five-stars:hover {width:130px;}
21#xzw_starSys .current-rating{ background: url(images/star.gif) left top; position: absolute; height: 20px; z-index: 1; top:0; left:0 }