[关闭]
@Bios 2019-06-28T06:50:35.000000Z 字数 3163 阅读 656

star.js

js


  1. (function(){
  2. //继承
  3. var extend = function(subClass,superClass){
  4. var F = function(){};
  5. F.prototype = superClass.prototype;
  6. subClass.prototype = new F();
  7. subClass.prototype.construtor = subClass;
  8. }
  9. //点亮
  10. var Light = function(el,options){
  11. this.$el = $(el);
  12. this.$item = this.$el.find('.rating-item');
  13. this.opts = options;
  14. this.add = 1;
  15. this.selectEvent = 'mouseover';
  16. }
  17. Light.prototype.init = function(){
  18. this.lightOn(this.opts.num);
  19. if(!this.opts.readOnly){
  20. this.bindEvent();
  21. }
  22. }
  23. Light.prototype.lightOn =function(num){
  24. var self = this;
  25. var num = parseInt(num);
  26. this.$item.each(function(index){
  27. if (index < num){
  28. $(this).css('background-position','0 -'+self.opts.y+'px');
  29. }else{
  30. $(this).css('background-position','0 0');
  31. }
  32. });
  33. }
  34. Light.prototype.bindEvent = function(e){
  35. var self = this,
  36. itemLength = self.$item.length;
  37. self.$el.on(self.selectEvent, '.rating-item', function(e) {
  38. var $this = $(this),
  39. num = 0;
  40. self.select(e,$this);
  41. num = $(this).index() + self.add;
  42. self.lightOn(num);
  43. (typeof self.opts.select === 'function') && self.opts.select.call(this,num,itemLength);
  44. self.$el.trigger('select',[num,itemLength]);
  45. }).on('click', '.rating-item', function() {
  46. self.opts.num = $(this).index() + self.add;
  47. (typeof self.opts.chosen === 'function') && self.opts.chosen.call(this,self.opts.num,itemLength);
  48. self.$el.trigger('chosen',[self.opts.num,itemLength]);
  49. }).on('mouseout', function() {
  50. self.lightOn(self.opts.num);
  51. })
  52. }
  53. Light.prototype.select = function(){
  54. throw new Error('子类必须重写此方法');
  55. }
  56. Light.prototype.unbindEvent = function(){
  57. this.$el.off();
  58. }
  59. //点亮整颗
  60. var LightEntire = function(el,options){
  61. Light.call(this,el,options)
  62. this.selectEvent = 'mouseover';
  63. }
  64. extend(LightEntire,Light);
  65. LightEntire.prototype.lightOn =function(num){
  66. Light.prototype.lightOn.call(this,num);
  67. }
  68. LightEntire.prototype.select = function(){
  69. self.add = 1;
  70. }
  71. //点亮半颗
  72. var LightHalf = function(el,options){
  73. Light.call(this,el,options)
  74. this.selectEvent = 'mousemove';
  75. }
  76. extend(LightHalf,Light);
  77. LightHalf.prototype.lightOn =function(num){
  78. self = this;
  79. var count = parseInt(num),
  80. isHalf = count !== num;
  81. Light.prototype.lightOn.call(this,count);
  82. if(isHalf){
  83. this.$item.eq(count).css('background-position','0 -'+self.opts.y*2+'px');
  84. }
  85. }
  86. LightHalf.prototype.select = function(e,$this){
  87. if(e.pageX - $this.offset().left < $this.width() / 2){
  88. this.add = 0.5;
  89. }else{
  90. this.add = 1;
  91. }
  92. }
  93. // 默认参数
  94. var defaults = {
  95. mode:'LightEntire',
  96. num:0,
  97. readOnly:false,
  98. y: 20,
  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. //option可以是object、string
  109. var $el = $(el),
  110. rating = $el.data('rating'),
  111. options = $.extend({},defaults,typeof option === 'object' && option);
  112. if(!mode[options.mode]){
  113. options.mode = 'LightEntire';
  114. }
  115. // new LightHalf(el,options).init();
  116. // new LightHalf(el,options).init();
  117. if(!rating!=''){
  118. //如果还没有实例化,就实例化
  119. $el.data('rating',(rating = new mode[options.mode](el,options)));
  120. rating.init();
  121. }
  122. if(typeof option === 'string'){
  123. rating[option]();
  124. }
  125. }
  126. $.fn.extend({
  127. rating:function(option){
  128. return this.each(function(){
  129. init(this,option);
  130. })
  131. }
  132. })
  133. return {
  134. init:init
  135. };
  136. })();
  1. <ul class="rating-max" id="rating1">
  2. <li class="rating-item" title="很不好"></li>
  3. <li class="rating-item" title="不好"></li>
  4. <li class="rating-item" title="一般"></li>
  5. <li class="rating-item" title="好"></li>
  6. <li class="rating-item" title="很好"></li>
  7. </ul>
  8. $('#rating1').rating({
  9. mode:'LightHalf',
  10. num:{{ course_info.get('score') }}/2,
  11. readOnly: true
  12. })
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注