使用jQuery实现点评星星效果

使用jQuery实现点评星星效果


 


 

 

没什么好说的,直接上代码吧

 

1. html代码

 

Html代码   收藏代码
  1. <table class="block">  
  2.     <tr>  
  3.         <td>  
  4.             <span class="label">总体评价<em>*</em>:</span>  
  5.         </td>  
  6.         <td>  
  7.             <div class="rating-wrap">  
  8.                 <ul class="rating-wrap-ul"  onmouseout="onUlMouseOut()" onmouseover="onUlMouseOver()">  
  9.                     <li><a class="one-star" title="很差" data-hint="很差" href="javascript:clickStar(1);" onmouseover="onLiMouseOver(1)" onmouseout="onLiMouseOut()"></a></li>  
  10.                     <li><a class="two-stars" title="差" data-hint="差" href="javascript:clickStar(2);" onmouseover="onLiMouseOver(2)" onmouseout="onLiMouseOut()"></a></li>  
  11.                     <li><a class="three-stars" title="还行" data-hint="还行" href="javascript:clickStar(3);" onmouseover="onLiMouseOver(3)" onmouseout="onLiMouseOut()"></a></li>  
  12.                     <li><a class="four-stars" title="好" data-hint="好" href="javascript:clickStar(4);" onmouseover="onLiMouseOver(4)" onmouseout="onLiMouseOut()"></a></li>  
  13.                     <li><a class="five-stars" title="很好" data-hint="很好" href="javascript:clickStar(5);" onmouseover="onLiMouseOver(5)" onmouseout="onLiMouseOut()"></a></li>  
  14.                 </ul>  
  15.             </div>  
  16.             <span id="ratingText" class="active-hint" innerText=""></span>  
  17.         </td>  
  18.     </tr>  
  19.     <tr>  
  20.         <td>  
  21.             <span class="label">评价<em>*</em>:</span>  
  22.         </td>  
  23.         <td>  
  24.             <span class="note">(50-2000个字)</span>  
  25.             <span id="textCount" class="note" innerText=""></span>  
  26.         </td>  
  27.     </tr>  
  28.     <tr>  
  29.         <td>  
  30.         </td>  
  31.         <td>  
  32.             <textarea name="appraiseText" id="appraiseText" class="form-content-block form-textarea" rows="12"></textarea>  
  33.         </td>  
  34.     </tr>  
  35.     <tr>  
  36.         <td>  
  37.         </td>  
  38.         <td align="right">  
  39.             <input type="button" value="提交" onclick="submitAppraise()">  
  40.             <input type="button" value="关闭" onclick=" ">  
  41.         </td>  
  42.     </tr>  
  43. </table>  

 

 

2. 所需要的css

 

Css代码   收藏代码
  1. body {  
  2.     color: #333;  
  3.     font: normal normal normal 12px/1.5 Arial, 宋体, sans-serif;  
  4. }  
  5. .block{  
  6.     clear: both;  
  7.     margin-bottom:20px;  
  8.     margin-bottom: 10px;  
  9.     zoom: 1;  
  10.     padding:5px 11px;border:1px solid #F5EEE8;  
  11.     padding-top:10px;margin:0 10px 0;  
  12.     padding-bottom:20px;border-bottom:1px dashed #E4E4E4;  
  13.     margin:10px auto;padding:0;border:none;  
  14. }  
  15.   
  16. .label{  
  17.     float:right;  
  18.     margin-right: 10px;  
  19.     text-align: right;  
  20.     font-weight: normal;  
  21.     font-style:normal;  
  22.     width: 94px;  
  23. }  
  24. em{  
  25.     margin-right:5px;  
  26.     color:#c00;  
  27.     font-weight:bold;  
  28.     font-style:normal;  
  29.     margin-left:2px;  
  30. }  
  31. .note {  
  32.     color: #999;  
  33. }  
  34.   
  35. .form-textarea{  
  36.     float: left;  
  37.     font-family: Tahoma, Geneva, sans-serif;  
  38.     margin-right: 5px;  
  39.     width: 598px;  
  40.     zoom: 1;  
  41.     font-family: inherit;  
  42.     font-size: 100%;  
  43.     -webkit-appearance: textarea;  
  44.     -webkit-box-orient: vertical;  
  45.     -webkit-rtl-ordering: logical;  
  46.     -webkit-user-select: text;  
  47.     background-color: white;  
  48.     border: 1px solid;  
  49.     cursor: auto;  
  50.     padding: 2px;  
  51.     resize: auto;  
  52.     white-space: pre-wrap;  
  53.     word-wrap: break-word;  
  54. }  
  55.           
  56. .rating-wrap {  
  57.     display: inline-block;  
  58.     float: left;  
  59.     position: relative;  
  60.     top: -2px;  
  61.     width: 89px;  
  62.     height: 20px;  
  63.     margin-right: 5px;  
  64.     padding: 4px 0 0 5px;  
  65.     border: 1px solid #EFE0D7;  
  66.     background: #FFF9F1;  
  67.     z-index: 0;  
  68. }  
  69. .rating-wrap ul,.rating-wrap a:hover {  
  70.     background-image: url(../images/star_shade.png);  
  71.     background-repeat: no-repeat;  
  72. }  
  73.   
  74. .rating-wrap ul {  
  75.     -webkit-padding-start: 40px;  
  76.     display: block;  
  77.     list-style-type: disc;  
  78.     margin: 1em 0px;  
  79.     border: 0px;  
  80.     margin: 0px;  
  81.     outline: 0px;  
  82.     padding: 0px;  
  83.     list-style: none;  
  84.     position: relative;  
  85.     width: 85px;  
  86.     height: 16px;  
  87.     background-position: 0 -90px;  
  88.     z-index: 10;  
  89. }  
  90.   
  91. .rating-wrap li {  
  92.     display: inline;  
  93. }  
  94.   
  95. .rating-wrap a {  
  96.     zoom: 1;  
  97.     position: absolute;  
  98.     left: 0;  
  99.     top: 0;  
  100.     display: block;  
  101.     height: 16px;  
  102. }  
  103.   
  104. .rating-wrap .five-stars {  
  105.     width: 84px;  
  106.     z-index: 10;  
  107.     background-position: 0 0px;  
  108. }  
  109.   
  110. .rating-wrap .four-stars {  
  111.     width: 68px;  
  112.     z-index: 20;  
  113.     background-position: 0 -18px;  
  114. }  
  115.   
  116. .rating-wrap .three-stars {  
  117.     width: 51px;  
  118.     z-index: 30;  
  119.     background-position: 0 -36px;  
  120. }  
  121.   
  122. .rating-wrap .two-stars {  
  123.     width: 34px;  
  124.     z-index: 40;  
  125.     background-position: 0 -54px;  
  126. }  
  127.   
  128. .rating-wrap .one-star {  
  129.     width: 17px;  
  130.     z-index: 50;  
  131.     background-position: 0 -72px;  
  132. }  
  133.   
  134. .active-hint{  
  135.     color: #C00;  
  136.     float: left;  
  137.     padding-top:2px;  
  138.     font-weight: normal;  
  139.     font-style:normal;  
  140. }  

 

 

3. 所需要的javascript代码

 

Js代码   收藏代码
  1. $(document).ready(function(){  
  2.     $("#appraiseText").bind("keydown"function(){  
  3.         var count = $("#appraiseText").val().length;  
  4.         if( count <= 200 ){  
  5.             $("#textCount").html(" 还能输入<font color='green'><b>" + (200 - count) + "</b></font>个字");  
  6.         }else{  
  7.             $("#textCount").html(" 已超出<font color='red'><b>" + (count - 200) + "</b></font>个字");  
  8.         }  
  9.     });   
  10. });  
  11.   
  12. var starValue=0;  
  13. function onUlMouseOut(){  
  14.     var y = -90 + starValue * 18;  
  15.     var position = "0 " + y + "px";  
  16.     $(".rating-wrap-ul").css({  
  17.         "background-position" : position  
  18.     });  
  19. }  
  20.   
  21. function onUlMouseOver(){  
  22.     $(".rating-wrap-ul").css({  
  23.         "background-position" : "0 -90px"  
  24.     });  
  25. }  
  26. function onLiMouseOver(grade){  
  27.     switch(grade){  
  28.         case 1 : document.getElementById("ratingText").innerHTML="很差";break;  
  29.         case 2 : document.getElementById("ratingText").innerHTML="差";break;  
  30.         case 3 : document.getElementById("ratingText").innerHTML="还行";break;  
  31.         case 4 : document.getElementById("ratingText").innerHTML="好";break;  
  32.         case 5 : document.getElementById("ratingText").innerHTML="很好";break;  
  33.         default :  document.getElementById("ratingText").innerHTML="";  
  34.     }  
  35. }  
  36. function onLiMouseOut(){  
  37.     onLiMouseOver(starValue);  
  38. }  
  39. function clickStar(grade){  
  40.     starValue = grade;        
  41.     var y = -90 + grade * 18;  
  42.     var position = "0 " + y + "px";  
  43.     $(".rating-wrap-ul").css({  
  44.         "background-position" : position  
  45.     });  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值