| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- /**
- * 点亮星星
- * */
- var rating = (function(){
- //继承
- var extend = function(subClass,superClass){
- var F = function(){};
- F.prototype = superClass.prototype;
- subClass.prototype = new F();
- subClass.prototype.constructor = subClass;
- }
- //点亮
- var Light = function(el,options){
- this.$el = $(el);
- this.$item = this.$el.find('.rating-item');
- this.opts = options;
- this.add = 1;
- this.selectEvent = 'mouseover';
- }
- Light.prototype.init = function(){
- this.lightOn(this.opts.num);
- if(!this.opts.readOnly){
- this.bindEvnet();
- }
- };
- Light.prototype.lightOn = function(num){
- num = parseInt(num);
- this.$item.each(function(index){
- if(index < num){
- $(this).css({'backgroundPosition':'0 0'});
- }else{
- $(this).css({'backgroundPosition':'0 -36px'});
- }
- });
- };
- Light.prototype.bindEvnet = function(){
- var self = this,
- itemLength = self.$item.length;
- this.$el.on(self.selectEvent,'.rating-item',function(e){
- var $this = $(this),
- num = 0;
- self.select(e,$this);
- num = $this.index() + self.add;
- self.lightOn(num);
-
- (typeof self.opts.select === 'function') && self.opts.select.call(this,num,itemLength);
- self.$el.trigger('select',[num,itemLength]);
- }).on('click','.rating-item',function(){
- self.opts.num = $(this).index() + self.add;
- (typeof self.opts.chosen === 'function') && self.opts.chosen.call(this,self.opts.num,itemLength);
-
- self.$el.trigger('chosen',[self.opts.num,itemLength]);
- }).on('mouseout',function(){
- self.lightOn(self.opts.num);
- });;
- };
- Light.prototype.select = function(){
- throw new Error('子类必须重写此方法!');
- };
- Light.prototype.unbindEvent = function(){
- this.$el.off();
- }
- //点亮整颗星
- var LightEntire = function(el,options){
- Light.call(this,el,options);
- this.selectEvent = 'mouseover';
- }
- extend(LightEntire,Light);
- LightEntire.prototype.lightOn = function(num){
- Light.prototype.lightOn.call(this,num);
- };
- LightEntire.prototype.select = function(){
- this.add = 1;
- }
- //点亮半颗星
- var LightHalf = function(el,options){
- Light.call(this,el,options);
- this.selectEvent = 'mousemove';
- }
- extend(LightHalf,Light);
- LightHalf.prototype.lightOn = function(num){
- var count = parseInt(num);
- var isHalf = count !== num;
- Light.prototype.lightOn.call(this,count);
- if(isHalf){
- this.$item.eq(count).css({'backgroundPosition':'0 -18px'});
- }
- };
- LightHalf.prototype.select = function(e,$this){
- if(e.pageX - $this.offset().left < $this.width()/2){
- this.add = .5;
- }else{
- this.add = 1
- }
- };
- // 默认参数
- var defaults = {
- mode: 'LightEntire',
- num: 0,
- readOnly: false,
- select:function(){},
- chosen:function(){}
- };
- var mode = {
- 'LightEntire': LightEntire,
- 'LightHalf': LightHalf
- }
- //初始化
- var init = function(el,option){
- var $el = $(el),
- rating = $el.data('rating'),
- options = $.extend({},defaults,typeof option === 'object' && option);
- if(!mode[options.mode]){
- options.mode = 'LightEntire';
- }
- if(!rating){
- $el.data('rating',(rating = new mode[options.mode](el,options)));
- rating.init();
- }
- if(typeof option === 'string'){
- rating[option]();
- }
-
- };
- //jQuery插件
- $.fn.extend({
- rating:function(option){
- return this.each(function(){
- init(this,option);
- })
- }
- });
- return {
- init:init
- }
- })();
- //用法
- //$('#rating1').rating({
- // mode:'LightHalf',
- // num: 3.5
- //});
- //$('#rating2').rating({
- // //mode:'LightHalf',
- // num: 3
- //});
- //$('#rating2').on('chosen',function(){
- // $('#rating2').rating('unbindEvent');
- //});
- /*rating.init('#rating1',{
- mode:'f',
- num:2.5,
- //readOnly:true
- //select:function(num,total){
- // console.log(this);
- // console.log('当前第' + num + '颗,一共' + total + '颗。');
- //},
- //chosen:function(num,total){
- // console.log(this);
- // console.log('当前点击的是第' + num + '颗,一共' + total + '颗。');
- //},
- chosen:function(){
- rating.init('#rating1','unbindEvent');
- }
- });*/
- /*$('#rating1').on('select',function(e,num,total){
- console.log('当前第' + num + '颗,一共' + total + '颗。');
- }).on('chosen',function(e,num,total){
- console.log('当前点击的是第' + num + '颗,一共' + total + '颗。');
- });*/
|