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中关于bind()方法的使用技巧分享
Mar 30 jQuery
jQuery 控制文本框自动缩小字体填充
Jun 16 jQuery
jQuery初级教程之网站品牌列表效果
Aug 02 jQuery
JavaScript实现离开页面前提示功能【附jQuery实现方法】
Sep 26 jQuery
jquery+ajaxform+springboot控件实现数据更新功能
Jan 22 jQuery
jQuery常见的遍历DOM操作详解
Sep 05 jQuery
jquery获取元素到屏幕四周可视距离的方法
Sep 05 jQuery
JQuery实现ajax请求的示例和注意事项
Dec 10 jQuery
jQuery实现的中英文切换功能示例
Jan 11 jQuery
jQuery实现条件搜索查询、实时取值及升降序排序的方法分析
May 04 jQuery
基于JavaScript或jQuery实现网站夜间/高亮模式
May 30 jQuery
基于ajax及jQuery实现局部刷新过程解析
Sep 12 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数字转汉字代码(算法)
2011/10/08 PHP
关于UEditor编辑器远程图片上传失败的解决办法
2012/08/31 PHP
php使用curl出现Expect:100-continue解决方法
2015/03/03 PHP
PHP获取一段文本显示点阵宽度和高度的方法
2015/03/12 PHP
Laravel实现自定义错误输出内容的方法
2016/10/10 PHP
PHP实现简易blog的制作
2016/10/24 PHP
javascript 面向对象思想 附源码
2009/07/07 Javascript
js表格分页实现代码
2009/09/18 Javascript
jQuery 树形结构的选择器
2010/02/15 Javascript
Javascript计算两个marker之间的距离(Google Map V3)
2013/04/26 Javascript
js与C#进行时间戳转换
2014/11/14 Javascript
探讨JavaScript语句的执行过程
2016/01/28 Javascript
完美JQuery图片切换效果的简单实现
2016/07/21 Javascript
使用vue框架 Ajax获取数据列表并用BootStrap显示出来
2017/04/24 Javascript
详解ES6通过WeakMap解决内存泄漏问题
2018/03/09 Javascript
Jquery 动态添加元素并添加点击事件实现过程解析
2019/10/12 jQuery
解决vuex数据异步造成初始化的时候没值报错问题
2019/11/13 Javascript
JavaScript实现文件下载并重命名代码实例
2019/12/12 Javascript
[02:38]2018DOTA2亚洲邀请赛赛前采访-VGJ.T
2018/04/03 DOTA
Python绘制3D图形
2018/05/03 Python
Pycharm创建项目时如何自动添加头部信息
2019/11/14 Python
基于python-pptx库中文文档及使用详解
2020/02/14 Python
Python拼接字符串的7种方式详解
2020/03/19 Python
Python正则re模块使用步骤及原理解析
2020/08/18 Python
Python selenium实现断言3种方法解析
2020/09/08 Python
css3一款3D字体带阴影效果的实现步骤
2013/03/20 HTML / CSS
使用Canvas操作像素的方法
2018/06/14 HTML / CSS
亚瑟士美国官网:ASICS美国
2017/02/01 全球购物
优秀英语专业毕业生求职信
2013/11/23 职场文书
小学校长先进事迹材料
2014/05/13 职场文书
学校班班通实施方案
2014/06/11 职场文书
公司合并协议书范本
2014/09/30 职场文书
农村结婚典礼主持词
2015/06/29 职场文书
python必学知识之文件操作(建议收藏)
2021/05/30 Python
Python 的 sum() Pythonic 的求和方法详细
2021/10/16 Python
python绘制云雨图raincloud plot
2022/08/05 Python