stars.js
6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
jQuery.fn.rater = function(options) {
// 默认参数
var settings = {
enabled : true,
url : '',
method : 'post',
min : 1,
max : 5,
step : 1,
value : null, //null
after_click : null,
before_ajax : null,
after_ajax : null,
title_format : null,
info_format : null,
image : 'stars.jpg',
imageAll :'stars-all.gif',
defaultTips :true,
clickTips :true,
width : 24,
height : 24
};
// 自定义参数
if(options) {
jQuery.extend(settings, options);
}
//外容器
var container = jQuery(this);
// 主容器
var content = jQuery('<ul class="rater-star"></ul>');
content.css('background-image' , 'url(' + settings.image + ')');
content.css('height' , settings.height);
content.css('width' , (settings.width*settings.step) * (settings.max-settings.min+settings.step)/settings.step);
//显示结果区域
var result= jQuery('<div class="rater-star-result"></div>');
container.after(result);
//显示点击提示
var clickTips= jQuery('<div class="rater-click-tips"></div>');
if(!settings.clickTips){
clickTips.hide();
}
container.after(clickTips);
//默认手形提示
var tipsItem= jQuery('<li class="rater-star-item-tips"></li>');
tipsItem.css('width' , (settings.width*settings.step) * (settings.max-settings.min+settings.step)/settings.step);
tipsItem.css('z-index' , settings.max / settings.step + 2);
if(!settings.defaultTips){ //隐藏默认的提示
tipsItem.hide();
}
content.append(tipsItem);
// 当前选中的
var item = jQuery('<li class="rater-star-item-current"></li>');
item.css('background-image' , 'url(' + settings.image + ')');
item.css('height' , settings.height);
item.css('width' , 0);
item.css('z-index' , settings.max / settings.step + 1);
if (settings.value) {
item.css('width' , ((settings.value-settings.min)/settings.step+1)*settings.step*settings.width);
};
content.append(item);
// 星星
for (var value=settings.min ; value<=settings.max ; value+=settings.step) {
item = jQuery('<li class="rater-star-item"><div class="popinfo"></div></li>');
if (typeof settings.info_format == 'function') {
//item.attr('title' , settings.title_format(value));
item.find(".popinfo").html(settings.info_format(value));
item.find(".popinfo").css("left",(value-1)*settings.width)
}
else {
item.attr('title' , value);
}
item.css('height' , settings.height);
item.css('width' , (value-settings.min+settings.step)*settings.width);
item.css('z-index' , (settings.max - value) / settings.step + 1);
item.css('background-image' , 'url(' + settings.image + ')');
if (!settings.enabled) { // 若是不能更改,则隐藏
item.hide();
}
content.append(item);
}
content.mouseover(function(){
if (settings.enabled) {
jQuery(this).find('.rater-star-item-current').hide();
}
}).mouseout(function(){
jQuery(this).find('.rater-star-item-current').show();
})
// 添加鼠标悬停/点击事件
var shappyWidth=(Math.round((settings.max+2)/2))*settings.width;
var happyWidth=(settings.max-1)*settings.width;
var fullWidth=settings.max*settings.width;
content.find('.rater-star-item').mouseover(function() {
jQuery(this).prevAll('.rater-star-item-tips').hide();
jQuery(this).attr('class' , 'rater-star-item-hover');
jQuery(this).find(".popinfo").show();
//一半以上用笑脸表示
if(parseInt(jQuery(this).css("width"))>=shappyWidth){
jQuery(this).addClass('rater-star-happy');
}
//当4分时用笑脸表示
if(parseInt(jQuery(this).css("width"))>=happyWidth){
jQuery(this).addClass('rater-star-happy');
}
//当5分时用笑脸表示
if(parseInt(jQuery(this).css("width"))==fullWidth){
jQuery(this).removeClass('rater-star-item-hover');
jQuery(this).css('background-image' , 'url(' + settings.imageAll + ')');
jQuery(this).css({cursor:'pointer',position:'absolute',left:'0',top:'0'});
}
}).mouseout(function() {
var outObj=jQuery(this);
outObj.css('background-image' , 'url(' + settings.image + ')');
outObj.attr('class' , 'rater-star-item');
outObj.find(".popinfo").hide();
outObj.removeClass('rater-star-happy');
jQuery(this).prevAll('.rater-star-item-tips').show();
//var startTip=function () {
//outObj.prevAll('.rater-star-item-tips').show();
//};
//startTip();
}).click(function() {
//jQuery(this).prevAll('.rater-star-item-tips').css('display','none');
jQuery(this).parents(".rater-star").find(".rater-star-item-tips").remove();
jQuery(this).parents(".goods-comm-stars").find(".rater-click-tips").remove();
jQuery(this).prevAll('.rater-star-item-current').css('width' , jQuery(this).width());
if(parseInt(jQuery(this).prevAll('.rater-star-item-current').css("width"))==happyWidth||parseInt(jQuery(this).prevAll('.rater-star-item-current').css("width"))==shappyWidth){
jQuery(this).prevAll('.rater-star-item-current').addClass('rater-star-happy');
}
else{
jQuery(this).prevAll('.rater-star-item-current').removeClass('rater-star-happy');
}
if(parseInt(jQuery(this).prevAll('.rater-star-item-current').css("width"))==fullWidth){
jQuery(this).prevAll('.rater-star-item-current').addClass('rater-star-full');
}
else{
jQuery(this).prevAll('.rater-star-item-current').removeClass('rater-star-full');
}
var star_count = (settings.max - settings.min) + settings.step;
var current_number = jQuery(this).prevAll('.rater-star-item').size()+1;
var current_value = settings.min + (current_number - 1) * settings.step;
//显示当前分值
if (typeof settings.title_format == 'function') {
jQuery(this).parents().nextAll('.rater-star-result').html(current_value+'分 '+settings.title_format(current_value));
}
//jQuery(this).parents().next('.rater-star-result').html(current_value);
//jQuery(this).unbind('mouseout',startTip)
})
jQuery(this).html(content);
}