现在的位置: 首页 > 综合 > 正文

评分设计鉴赏

2013年08月17日 ⁄ 综合 ⁄ 共 3597字 ⁄ 字号 评论关闭

最近看了几个国外网站的评分设计.觉得mp3.com作的最值的学习,内容(html),行为(javascript),表现(CSS)将三者设计的恰到妙处.
评分的原理大致如下:
一张5星级的图片或5张一星的图片连接在一起,每一个星位上有一个事件函数把相应的星值和当前的记录ID通过ajax送到后台更新.mp3.com设计的妙点在:
1:用了一张星图,亮暗几占星图总高度的一半:
示例星图地址:http://image.com.com/mp3/images/stars/star-rating-bg.gif
2:评分设计中,移动鼠标显示相应的星值,一定会用到JS事件函数来改变星图.mp3.com在这步也作足了功夫
用8个处理函数打成一个rating.js,个人觉得最棒的是人家没在html中硬编码.
3.显示星值.这个部分不及前2个妙点.不过也有可圈可点之处.
示例代码从mp3.com中扣的,用到了prototype框架,没有服务器端代码.这个相信难不住大家.

示例代码:html

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <script type="text/javascript" src="http://localhost/demo/include/js/prototype.js"></script>
  6. <title>无标题文档</title>
  7. </head>
  8. <body>
  9. <div id="sitepage">
  10.     <div class="star-rating fr">
  11.         <div style="width: 84%;"></div>
  12.     </div>
  13.     <div id="vote-1-49320" class="star-rating fr">
  14.         <div class="0" style="width: 0%;"></div>
  15.             <a href="http://www.mp3.com/signup" title="0.5 stars!" rev="vote-against" name="1-49320-1"></a>
  16.             <a href="http://www.mp3.com/signup" title="1 stars!" rev="vote-against" name="1-49320-2"></a>
  17.             <a href="http://www.mp3.com/signup" title="1.5 stars!" rev="vote-against" name="1-49320-3"></a>
  18.             <a href="http://www.mp3.com/signup" title="2 stars!" rev="vote-against" name="1-49320-4"></a>
  19.             <a href="http://www.mp3.com/signup" title="2.5 stars!" rev="vote-against" name="1-49320-5"></a>
  20.             <a href="http://www.mp3.com/signup" title="3 stars!" rev="vote-for" name="1-49320-6"></a>
  21.             <a href="http://www.mp3.com/signup" title="3.5 stars!" rev="vote-for" name="1-49320-7"></a>
  22.             <a href="http://www.mp3.com/signup" title="4 stars!" rev="vote-for" name="1-49320-8"></a>
  23.             <a href="http://www.mp3.com/signup" title="4.5 stars!" rev="vote-for" name="1-49320-9"></a>
  24.             <a href="http://www.mp3.com/signup" title="5 stars!" rev="vote-for" name="1-49320-10"></a>      
  25.             <img id="startAddVoteListeners-1-49320" class="anon" src="http://image.com.com/mp3/images/b.gif" style="position:absolute;" alt="Load this to turn on javascript" />
  26.     </div>
  27.     <script type="text/javascript">// let's get this party started!
  28.     if ($('vote-1-49320')) {
  29.     addVoteListeners(1,49320);
  30.     // already voted
  31.     if ($('startAddVoteListeners-1-49320').className != '' && $('startAddVoteListeners-1-49320').className != 'anon') {
  32.         toggleListeners('none', 1, 49320);
  33.     }}
  34.     </script>
  35. </div>
  36. </body>
  37. </html>

代码的显示会偏右,mp3.com的评分就在哪个方位.我没改.

 

示例代码:CSS

  1. <style type="text/css">
  2. .star-rating {
  3. width140px;
  4. height30px;
  5. positionrelative;
  6. backgroundurl(http://image.com.com/mp3/images/stars/star-rating-bg.gif) bottom;
  7. z-index1;
  8. }
  9. .star-rating div {
  10. width0;
  11. height30px;
  12. positionabsolute;
  13. left: 0;
  14. top: 0;
  15. backgroundurl(http://image.com.com/mp3/images/stars/star-rating-bg.gif) top left;
  16. z-index2;
  17. }
  18. .star-rating a {
  19. width10%;
  20. height30px;
  21. floatleft;
  22. positionrelative;
  23. z-index3;
  24. }
  25. .fr {floatright;}
  26. .fl {floatleft;}
  27. .fl,.fr {positionrelative;}
  28. </style>

示例代码:javascript

有9个函数,其中8个在mp3.com是放在一起的rating.js,您也可以去mp3.com下载,包含到html中即可.下载地址:

http://www.mp3.com/js/ratings.js

您看到的是全局的事件函数:

  1. function addEvent(obj, evType, fn, useCapture){
  2.     if (obj.addEventListener){
  3.         obj.addEventListener(evType, fn, useCapture);
  4.         return true;
  5.     }
  6.     else if (obj.attachEvent) {
  7.         var r = obj.attachEvent("on"+evType, fn);
  8.         return r;
  9.     }
  10.     else {
  11.         alert("Handler could not be attached");
  12.     }
  13. }

 

现在才真的体会到:看好的代码是一种享受.希望您看到这篇文章可以学到更多的设计思想

抱歉!评论已关闭.