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请求servlet实现ajax异步请求的示例
Jun 03 jQuery
jQuery.Ajax()的data参数类型详解
Jul 23 jQuery
关于JS与jQuery中的文档加载问题
Aug 22 jQuery
vue.js2.0点击获取自己的属性和jquery方法
Feb 23 jQuery
jQuery+CSS实现的标签页效果示例【测试可用】
Aug 14 jQuery
jQuery利用FormData上传文件实现批量上传
Dec 04 jQuery
jQuery对底部导航进行跳转并高亮显示的实例代码
Apr 23 jQuery
基于jquery ajax的多文件上传进度条过程解析
Sep 11 jQuery
jquery绑定事件 bind和on的用法与区别分析
May 22 jQuery
JQuery插件tablesorter表格排序实现过程解析
May 28 jQuery
JQuery基于FormData异步提交数据文件
Sep 01 jQuery
JS实现选项卡插件的两种写法(jQuery和class)
Dec 30 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错误Cannot use object of type stdClass as array in错误的解决办法
2014/06/12 PHP
destoon整合UCenter图文教程
2014/06/21 PHP
PHP中变量引用与变量销毁机制分析
2014/11/15 PHP
php通过asort()给关联数组按照值排序的方法
2015/03/18 PHP
微信自定义分享php代码分析
2016/11/24 PHP
laravel框架模型中非静态方法也能静态调用的原理分析
2019/11/23 PHP
javascript 对象的定义方法
2007/01/10 Javascript
各浏览器对link标签onload/onreadystatechange事件支持的差异分析
2011/04/27 Javascript
JS中产生标识符方式的演变
2015/06/12 Javascript
javascript实现简单的全选和反选功能
2016/01/05 Javascript
AngularJs 60分钟入门基础教程
2016/04/03 Javascript
jQuery图片左右滚动代码 有左右按钮实例
2016/06/20 Javascript
js实现可输入可选择的select下拉框
2016/12/21 Javascript
ES6概念 Symbol toString()方法
2016/12/25 Javascript
ES6入门教程之let和const命令详解
2017/05/17 Javascript
Vue中定义全局变量与常量的各种方式详解
2017/08/23 Javascript
vue打包的时候自动将px转成rem的操作方法
2018/06/20 Javascript
详解如何在vue项目中使用eslint+prettier格式化代码
2018/11/10 Javascript
手动下载Chrome并解决puppeteer无法使用问题
2018/11/12 Javascript
Vue使用JSEncrypt实现rsa加密及挂载方法
2020/02/07 Javascript
Python实现PS图像明亮度调整效果示例
2018/01/23 Python
Python实现的根据IP地址计算子网掩码位数功能示例
2018/05/23 Python
Python实现string字符串连接的方法总结【8种方式】
2018/07/06 Python
Python图像处理实现两幅图像合成一幅图像的方法【测试可用】
2019/01/04 Python
Numpy中对向量、矩阵的使用详解
2019/10/29 Python
python基于plotly实现画饼状图代码实例
2019/12/16 Python
python爬虫利用代理池更换IP的方法步骤
2021/02/21 Python
css3实现书本翻页效果的示例代码
2021/03/08 HTML / CSS
HTML5+CSS3模仿优酷视频截图功能示例
2017/01/05 HTML / CSS
美国祛痘、抗衰老药妆品牌:Murad
2016/08/27 全球购物
特步官方商城:Xtep
2017/03/21 全球购物
英国领先的高街书籍专家:Waterstones
2018/02/01 全球购物
体育教育专业毕业生自荐信
2013/11/15 职场文书
教师学习培训邀请函
2014/02/04 职场文书
外联部演讲稿
2014/05/24 职场文书
党员学习中共十八大报告思想汇报
2014/09/15 职场文书