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实现在HTML文档加载完毕后自动执行某个事件的方法
May 08 jQuery
详谈表单格式化插件jquery.serializeJSON
Jun 23 jQuery
jQuery实现弹窗下底部页面禁止滑动效果
Dec 19 jQuery
jQuery中each方法的使用详解
Mar 18 jQuery
jQuery中的$是什么意思及 $. 和 $().的区别
Apr 20 jQuery
jQuery实现点击旋转,再点击恢复初始状态动画效果示例
Dec 11 jQuery
jQuery实现input输入框获取焦点与失去焦点时提示的消失与显示功能示例
May 27 jQuery
jQuery与原生JavaScript选择HTML元素集合用法对比分析
Nov 26 jQuery
jquery实现直播弹幕效果
Nov 28 jQuery
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
Apr 30 jQuery
jQuery实现简单日历效果
Jul 05 jQuery
基于jQuery拖拽事件的封装
Nov 29 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写的获取各搜索蜘蛛爬行记录代码
2012/08/21 PHP
PHP使用SWOOLE扩展实现定时同步 MySQL 数据
2017/04/09 PHP
PHP PDO和消息队列的个人理解与应用实例分析
2019/11/25 PHP
解决jQuery插件tipswindown与hintbox冲突
2010/11/05 Javascript
原始的js代码和jquery对比体会
2013/09/10 Javascript
在页面上用action传递参数到后台出现乱码的解决方法
2013/12/31 Javascript
javascript生成不重复的随机数
2015/07/17 Javascript
Node.js模块封装及使用方法
2016/03/06 Javascript
ionic js 复选框 与普通的 HTML 复选框到底有没区别
2016/06/06 Javascript
JavaScript重复元素处理方法分析【统计个数、计算、去重复等】
2017/12/14 Javascript
Angular入口组件(entry component)与声明式组件的区别详解
2018/04/09 Javascript
Angularjs之ngModel中的值验证绑定方法
2018/09/13 Javascript
javascript浅层克隆、深度克隆对比及实例解析
2020/02/09 Javascript
html-webpack-plugin修改页面的title的方法
2020/06/18 Javascript
手机浏览器唤起微信分享(JS)
2020/10/11 Javascript
[47:22]Mineski vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
pyqt和pyside开发图形化界面
2014/01/22 Python
Python3实现从指定路径查找文件的方法
2015/05/22 Python
Python实现模拟登录及表单提交的方法
2015/07/25 Python
基于python中的TCP及UDP(详解)
2017/11/06 Python
Python编程实现蚁群算法详解
2017/11/13 Python
Python简单计算文件MD5值的方法示例
2018/04/11 Python
解决Python中list里的中文输出到html模板里的问题
2018/12/17 Python
详解python中的线程与线程池
2019/05/10 Python
详解Python中的正斜杠与反斜杠
2019/08/09 Python
关于keras.layers.Conv1D的kernel_size参数使用介绍
2020/05/22 Python
Pandas DataFrame求差集的示例代码
2020/12/13 Python
瑞士领先的网上超市:LeShop.ch
2018/11/14 全球购物
加大码胸罩、内裤和服装:Just My Size
2019/03/21 全球购物
网站域名和主机:Domain.com
2019/04/01 全球购物
导游的职业规划书范文
2013/12/27 职场文书
合作经营协议书
2014/04/17 职场文书
2015年社区宣传工作总结
2015/05/20 职场文书
老干部局2015年度工作总结
2015/10/22 职场文书
python使用glob检索文件的操作
2021/05/20 Python
pandas数值排序的实现实例
2021/07/25 Python