jQuery Raty星级评分插件使用方法实例分析


Posted in jQuery onNovember 25, 2019

本文实例讲述了jQuery Raty星级评分插件使用方法。分享给大家供大家参考,具体如下:

使用jQuery Raty,可以很方便的在页面上嵌入一个评分组件,如下所示:

使用方法很简单,首先从https://github.com/wbotelhos/raty下载raty的源代码(依赖于jquery)

然后在页面中引入相应的js文件、css文件、图片资源,在需要添加评分组件的元素上(比如span标签)添加下面的jquery代码即可:

$('span').raty();

以上为jQuery Raty的缺省使用方法,此外,该组件还支持丰富的传入参数和回调函数,例如:

设置jQuery Raty的初始评分:

评分回调函数

如果需要根据后台动态设置初始评分,可以使用回调函数实现。例如使用div中的data-attribute属性:

<div data-score="1"></div>
$('div').raty({
 score: function() {
  return $(this).attr('data-score');
 }
});

还可以改变星星的个数:

$('div').raty({ number: 10 });

只读模式:

$('div').raty({ readOnly: true, score: 3 });

点击事件:

$('div').raty({
 click: function(score, evt) {
  alert('ID: ' + this.id + "\nscore: " + score + "\nevent: " + evt);
 }
});

路径:

变更图标保存的位置,所有图标需要位于同一目录下,路径结尾的/不添加也可以

<div data-path="assets/images"></div>
$('div').raty({
 path: function() {
  return this.getAttribute('data-path');
 }
});

取消评分:

$('div').raty({ cancel: true });

全局改变设置:

你可以全局更改上述提到的所有设置 $.fn.raty.defaults.OPTION = VALUE;. 该语句必须添加在插件绑定之前。

$.fn.raty.defaults.path = assets;
$.fn.raty.defaults.cancel = true;

参数列表:

cancel   : false                     // Creates a cancel button to cancel the rating.
cancelClass : 'raty-cancel'                 // Name of cancel's class.
cancelHint : 'Cancel this rating!'             // The cancel's button hint.
cancelOff  : 'cancel-off.png'                // Icon used on active cancel.
cancelOn  : 'cancel-on.png'                // Icon used inactive cancel.
cancelPlace : 'left'                     // Cancel's button position.
click    : undefined                   // Callback executed on rating click.
half    : false                     // Enables half star selection.
halfShow  : true                      // Enables half star display.
hints    : ['bad', 'poor', 'regular', 'good', 'gorgeous'] // Hints used on each star.
iconRange  : undefined                   // Object list with position and icon on and off to do a mixed icons.
mouseout  : undefined                   // Callback executed on mouseout.
mouseover  : undefined                   // Callback executed on mouseover.
noRatedMsg : 'Not rated yet!'                // Hint for no rated elements when it's readOnly.
number   : 5                       // Number of stars that will be presented.
numberMax  : 20                       // Max of star the option number can creates.
path    : undefined                   // A global locate where the icon will be looked.
precision  : false                     // Enables the selection of a precision score.
readOnly  : false                     // Turns the rating read-only.
round    : { down: .25, full: .6, up: .76 }        // Included values attributes to do the score round math.
score    : undefined                   // Initial rating.
scoreName  : 'score'                    // Name of the hidden field that holds the score value.
single   : false                     // Enables just a single star selection.
space    : true                      // Puts space between the icons.
starHalf  : 'star-half.png'                // The name of the half star image.
starOff   : 'star-off.png'                 // Name of the star image off.
starOn   : 'star-on.png'                 // Name of the star image on.
target   : undefined                   // Element selector where the score will be displayed.
targetFormat: '{score}'                   // Template to interpolate the score in.
targetKeep : false                     // If the last rating value will be keeped after mouseout.
targetScore : undefined                   // Element selector where the score will be filled, instead of creating a new hidden field (scoreName option).
targetText : ''                       // Default text setted on target.
targetType : 'hint'                     // Option to choose if target will receive hint o 'score' type.
starType  : 'img'                     // Element used to represent a star.

回调函数列表:

$('div').raty('score');         // Get the current score.
$('div').raty('score', number);     // Set the score.
$('div').raty('click', number);     // Click on some star.
$('div').raty('readOnly', boolean);   // Change the read-only state.
$('div').raty('cancel', boolean);    // Cancel the rating. The last param force the click callback.
$('div').raty('reload');         // Reload the rating with the current configuration.
$('div').raty('set', { option: value }); // Reset the rating with new configurations.
$('div').raty('destroy');        // Destroy the bind and give you the raw element.
$('div').raty('move', number);      // Move the mouse to the given score point position.

希望本文所述对大家jQuery程序设计有所帮助。

jQuery 相关文章推荐
jQuery实现字体颜色渐变效果的方法
Mar 29 jQuery
Jquery把获取到的input值转换成json
May 15 jQuery
jQuery实现选中行变色效果(实例讲解)
Jul 06 jQuery
jQuery实现倒计时功能 jQuery实现计时器功能
Sep 19 jQuery
jQuery中过滤器的基本用法示例
Oct 11 jQuery
jQuery除指定区域外点击任何地方隐藏DIV功能
Nov 13 jQuery
jQuery实现验证表单密码一致性及正则表达式验证邮箱、手机号的方法
Dec 05 jQuery
用jquery获取select标签中选中的option值及文本的示例
Jan 25 jQuery
浅谈jquery fullpage 插件增加头部和版权的方法
Mar 20 jQuery
使用JQuery自动完成插件Auto Complete详解
Jun 18 jQuery
jquery实现轮播图特效
Apr 12 jQuery
jQuery实现简单轮播图效果
Dec 27 jQuery
jquery 插件重新绑定的处理方法分析
Nov 23 #jQuery
JQuery使用属性addClass、removeClass和toggleClass实现增加和删除类操作示例
Nov 18 #jQuery
Jquery让form表单异步提交代码实现
Nov 14 #jQuery
JQuery发送ajax请求时中文乱码问题解决
Nov 14 #jQuery
Jquery异步上传文件代码实例
Nov 13 #jQuery
jQuery实现滑动星星评分效果(每日分享)
Nov 13 #jQuery
jquery获取input输入框中的值
Nov 13 #jQuery
You might like
php实现水仙花数的4个示例分享
2014/04/08 PHP
PHP生成二维码的两个方法和实例
2014/07/01 PHP
基于php伪静态的实现方法解析
2020/07/31 PHP
jQuery中:checked选择器用法实例
2015/01/04 Javascript
Jquery轮播效果实现过程解析
2016/03/30 Javascript
Bootstrap显示与隐藏简单实现代码
2017/03/06 Javascript
js读取json文件片段中的数据实例
2017/03/09 Javascript
微信小程序实现移动端滑动分页效果(ajax)
2017/06/13 Javascript
JavaScript对JSON数据进行排序和搜索
2017/07/24 Javascript
vue2实现可复用的轮播图carousel组件详解
2017/11/27 Javascript
手挽手带你学React之React-router4.x的使用
2019/02/14 Javascript
Nodejs实现微信分账的示例代码
2021/01/19 NodeJs
[01:14]DOTA2亚洲邀请赛小组赛赛前花絮
2017/03/27 DOTA
[25:45]2018DOTA2亚洲邀请赛4.5SOLO赛 Sylar vs Paparazi
2018/04/06 DOTA
[47:35]VP vs Pain 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/20 DOTA
自己使用总结Python程序代码片段
2015/06/02 Python
今天 平安夜 Python 送你一顶圣诞帽 @微信官方
2017/12/25 Python
python无限生成不重复(字母,数字,字符)组合的方法
2018/12/04 Python
python使用matplotlib画柱状图、散点图
2019/03/18 Python
Python安装whl文件过程图解
2020/02/18 Python
python实现低通滤波器代码
2020/02/26 Python
Python3+Flask安装使用教程详解
2021/02/16 Python
web页面录屏实现
2019/02/12 HTML / CSS
html5的input的required使用中遇到的问题及解决方法
2018/04/24 HTML / CSS
加拿大奢华时装品牌:Mackage
2018/01/10 全球购物
生物有机护肤品:Aurelia Probiotic Skincare
2018/01/31 全球购物
DC Shoes俄罗斯官网:美国滑板鞋和服饰品牌
2020/08/19 全球购物
会展中心部门工作职责
2013/11/27 职场文书
在校大学生个人的自我评价
2014/02/13 职场文书
计算机通信专业推荐信
2014/02/22 职场文书
青奥会口号
2014/06/12 职场文书
优秀本科毕业生自荐信
2014/07/04 职场文书
农民工预备党员思想汇报
2014/09/14 职场文书
利用python做表格数据处理
2021/04/13 Python
Python3中最常用的5种线程锁实例总结
2021/07/07 Python
常用的文件对应的MIME类型汇总
2022/04/26 HTML / CSS