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平滑滚动到顶部插件使用详解
May 08 jQuery
jQuery Validate 无法验证 chosen-select元素的解决方法
May 17 jQuery
jQuery+ajax实现局部刷新的两种方法
Jun 08 jQuery
基于jQuery对象和DOM对象和字符串之间的转化实例
Aug 08 jQuery
使用jquery的jsonp如何发起跨域请求及其原理详解
Aug 17 jQuery
JQuery判断正整数整理小结
Aug 21 jQuery
jQuery实现带右侧索引功能的通讯录示例【附源码下载】
Apr 17 jQuery
jQuery超简单遮罩层实现方法示例
Sep 06 jQuery
详解jQuery获取特殊属性的值以及设置内容
Nov 14 jQuery
jquery分页优化操作实例分析
Aug 23 jQuery
详解jQuery的核心函数和事件处理
Feb 18 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
第十五节--Zend引擎的发展
2006/11/16 PHP
QQ登录 PHP OAuth示例代码
2011/07/20 PHP
如何获知PHP程序占用多少内存(memory_get_usage)
2012/09/23 PHP
zf框架的registry(注册表)使用示例
2014/03/13 PHP
PHP获取中英混合字符串长度的方法
2014/06/07 PHP
wordpress安装过程中遇到中文乱码的处理方法
2015/04/21 PHP
php析构函数的简单使用说明
2015/08/24 PHP
详解关于php的xdebug配置(编辑器vscode)
2019/01/29 PHP
js用图作提交按钮或超连接
2008/03/26 Javascript
jquery之empty()与remove()区别说明
2010/09/10 Javascript
解决js正则匹配换行问题实现代码
2012/12/10 Javascript
jquery获取子节点和父节点的示例代码
2013/09/10 Javascript
JS实现自适应高度表单文本框的方法
2015/02/25 Javascript
Javascript仿京东放大镜的效果
2017/03/01 Javascript
nodejs实现的连接MySQL数据库功能示例
2018/01/25 NodeJs
基于vue.js实现分页查询功能
2018/12/29 Javascript
iphone刘海屏页面适配方法
2019/05/07 Javascript
webpack4手动搭建Vue开发环境实现todoList项目的方法
2019/05/16 Javascript
elementUI vue this.$confirm 和el-dialog 弹出框 移动 示例demo
2019/07/03 Javascript
如何利用node转发请求详解
2020/09/17 Javascript
Vue如何实现变量表达式选择器
2021/02/18 Vue.js
[01:12](回顾)DOTA2国际邀请赛,全世界DOTAer的盛宴
2014/07/01 DOTA
[01:13]2015国际邀请赛线下观战现场
2015/08/08 DOTA
Pycharm 设置默认头的图文教程
2019/01/17 Python
python实现基于朴素贝叶斯的垃圾分类算法
2019/07/09 Python
django 快速启动数据库客户端程序的方法示例
2019/08/16 Python
pycharm永久激活超详细教程
2020/10/29 Python
ProBikeKit澳大利亚:自行车套件,跑步和铁人三项装备
2016/11/30 全球购物
JPA的优势都有哪些
2013/07/04 面试题
公交公司毕业生求职信
2014/02/15 职场文书
2014年干部作风建设总结
2014/10/23 职场文书
医德医风个人工作总结2014
2014/11/14 职场文书
2015年八一建军节活动总结
2015/03/20 职场文书
《观潮》教学反思
2016/02/17 职场文书
python中__slots__节约内存的具体做法
2021/07/04 Python
Python爬虫 简单介绍一下Xpath及使用
2022/04/26 Python