Nenhuma Descrição

ratingStars.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * 点亮星星
  3. * */
  4. var rating = (function(){
  5. //继承
  6. var extend = function(subClass,superClass){
  7. var F = function(){};
  8. F.prototype = superClass.prototype;
  9. subClass.prototype = new F();
  10. subClass.prototype.constructor = subClass;
  11. }
  12. //点亮
  13. var Light = function(el,options){
  14. this.$el = $(el);
  15. this.$item = this.$el.find('.rating-item');
  16. this.opts = options;
  17. this.add = 1;
  18. this.selectEvent = 'mouseover';
  19. }
  20. Light.prototype.init = function(){
  21. this.lightOn(this.opts.num);
  22. if(!this.opts.readOnly){
  23. this.bindEvnet();
  24. }
  25. };
  26. Light.prototype.lightOn = function(num){
  27. num = parseInt(num);
  28. this.$item.each(function(index){
  29. if(index < num){
  30. $(this).css({'backgroundPosition':'0 0'});
  31. }else{
  32. $(this).css({'backgroundPosition':'0 -36px'});
  33. }
  34. });
  35. };
  36. Light.prototype.bindEvnet = function(){
  37. var self = this,
  38. itemLength = self.$item.length;
  39. this.$el.on(self.selectEvent,'.rating-item',function(e){
  40. var $this = $(this),
  41. num = 0;
  42. self.select(e,$this);
  43. num = $this.index() + self.add;
  44. self.lightOn(num);
  45. (typeof self.opts.select === 'function') && self.opts.select.call(this,num,itemLength);
  46. self.$el.trigger('select',[num,itemLength]);
  47. }).on('click','.rating-item',function(){
  48. self.opts.num = $(this).index() + self.add;
  49. (typeof self.opts.chosen === 'function') && self.opts.chosen.call(this,self.opts.num,itemLength);
  50. self.$el.trigger('chosen',[self.opts.num,itemLength]);
  51. }).on('mouseout',function(){
  52. self.lightOn(self.opts.num);
  53. });;
  54. };
  55. Light.prototype.select = function(){
  56. throw new Error('子类必须重写此方法!');
  57. };
  58. Light.prototype.unbindEvent = function(){
  59. this.$el.off();
  60. }
  61. //点亮整颗星
  62. var LightEntire = function(el,options){
  63. Light.call(this,el,options);
  64. this.selectEvent = 'mouseover';
  65. }
  66. extend(LightEntire,Light);
  67. LightEntire.prototype.lightOn = function(num){
  68. Light.prototype.lightOn.call(this,num);
  69. };
  70. LightEntire.prototype.select = function(){
  71. this.add = 1;
  72. }
  73. //点亮半颗星
  74. var LightHalf = function(el,options){
  75. Light.call(this,el,options);
  76. this.selectEvent = 'mousemove';
  77. }
  78. extend(LightHalf,Light);
  79. LightHalf.prototype.lightOn = function(num){
  80. var count = parseInt(num);
  81. var isHalf = count !== num;
  82. Light.prototype.lightOn.call(this,count);
  83. if(isHalf){
  84. this.$item.eq(count).css({'backgroundPosition':'0 -18px'});
  85. }
  86. };
  87. LightHalf.prototype.select = function(e,$this){
  88. if(e.pageX - $this.offset().left < $this.width()/2){
  89. this.add = .5;
  90. }else{
  91. this.add = 1
  92. }
  93. };
  94. // 默认参数
  95. var defaults = {
  96. mode: 'LightEntire',
  97. num: 0,
  98. readOnly: false,
  99. select:function(){},
  100. chosen:function(){}
  101. };
  102. var mode = {
  103. 'LightEntire': LightEntire,
  104. 'LightHalf': LightHalf
  105. }
  106. //初始化
  107. var init = function(el,option){
  108. var $el = $(el),
  109. rating = $el.data('rating'),
  110. options = $.extend({},defaults,typeof option === 'object' && option);
  111. if(!mode[options.mode]){
  112. options.mode = 'LightEntire';
  113. }
  114. if(!rating){
  115. $el.data('rating',(rating = new mode[options.mode](el,options)));
  116. rating.init();
  117. }
  118. if(typeof option === 'string'){
  119. rating[option]();
  120. }
  121. };
  122. //jQuery插件
  123. $.fn.extend({
  124. rating:function(option){
  125. return this.each(function(){
  126. init(this,option);
  127. })
  128. }
  129. });
  130. return {
  131. init:init
  132. }
  133. })();
  134. //用法
  135. //$('#rating1').rating({
  136. // mode:'LightHalf',
  137. // num: 3.5
  138. //});
  139. //$('#rating2').rating({
  140. // //mode:'LightHalf',
  141. // num: 3
  142. //});
  143. //$('#rating2').on('chosen',function(){
  144. // $('#rating2').rating('unbindEvent');
  145. //});
  146. /*rating.init('#rating1',{
  147. mode:'f',
  148. num:2.5,
  149. //readOnly:true
  150. //select:function(num,total){
  151. // console.log(this);
  152. // console.log('当前第' + num + '颗,一共' + total + '颗。');
  153. //},
  154. //chosen:function(num,total){
  155. // console.log(this);
  156. // console.log('当前点击的是第' + num + '颗,一共' + total + '颗。');
  157. //},
  158. chosen:function(){
  159. rating.init('#rating1','unbindEvent');
  160. }
  161. });*/
  162. /*$('#rating1').on('select',function(e,num,total){
  163. console.log('当前第' + num + '颗,一共' + total + '颗。');
  164. }).on('chosen',function(e,num,total){
  165. console.log('当前点击的是第' + num + '颗,一共' + total + '颗。');
  166. });*/