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.

本文转载,链接地址:http://bookshadow.com/weblog/2014/08/16/jquery-raty-star-plugin/

时间: 2024-07-28 12:43:57

jQuery Raty星级评分插件使用方法的相关文章

jQuery Raty 星级评分

在线实例 实例演示 使用方法 <div id="star"></div> 复制 $('#star').raty(); 复制 下载

jQuery的validate验证插件使用方法

(1)默认校验规则(1)required:true 必输字段(2)remote:"check.php" 使用ajax方法调用check.php验证输入值(3)email:true 必须输入正确格式的电子邮件(4)url:true 必须输入正确格式的网址(5)date:true 必须输入正确格式的日期(6)dateISO:true 必须输入正确格式的日期(ISO),例如:2009-06-23,1998/01/22 只验证格式,不验证有效性(7)number:true 必须输入合法的数字(

jQuery Raty - 不错的星级评分插件

在做商品评价时,一般情况下,我们需要一个星级评分的组件,而jQuery Raty恰好满足我们的需求. 一.展示 二.使用教程 ①.下载插件 https://github.com/wbotelhos/raty ②.导入文件 <script type="text/javascript" src="${ctx}/components/raty/jquery.raty.js"></script> <link type="text/cs

js的选择星级评分插件

需要引入jquery和raty的js,并且低版本的jquery可能会出现样式问题,或者点击没反应,可换高版本试试 raty文档及下载: http://www.wbotelhos.com/raty/ 把下载后文件夹中的lib下文件拷到自己项目下 1 <script language="javascript" type="text/javascript" src="/JavaScript/jquery/jquery-2.1.0.min.js"&

Jquery UI的datepicker插件使用方法

原文链接;http://www.ido321.com/375.html Jquery UI是一个非常丰富的Jquery插件,并且UI的各部分插件可以独自分离出来使用,这是其他很多Jquery插件没有的优势.最近对UI中的datepicker插件学习了一下,这款日期选择/日历显示插件很好用.废话不多说,先来张图,看看效果: <span style="font-size:18px;"><!DOCTYPE> <html> <head> <

jquery实现星级评分,鼠标移入和移出改变评分

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>demo3.html</title> 5 6 <meta name="keywords" content="keyword1,keyword2,keyword3"> 7 <meta name="description" content="this is my page&q

js实现星级评分之方法一

利用一个星级评分的小案例,来逐步封装js星级评分插件. 从最基础的js知识,通过一个小的demo,逐步学习js的面向对象知识. 从浅到深,逐步递进. 图片素材 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>星级评分--第一种实现方式</title> </head> <style type="text/css&quo

评分插件 jquery.raty.js

本来想用bootstrap-star-rating的,就是上传插件那个组做的.但是还是css的问题 后来百度了个别的东西,第一页就能搜到,就不妨链接了. * @version        2.5.2 * @since          2010.06.11 * @author         Washington Botelho * @documentation  wbotelhos.com/raty 很老的一个东西,能用就好. <div id="star{{ite.$id}}"

jQuery 评分插件(转)

评分效果的小插件jQuery Raty.它提供的API相当丰富真的是让人爱不释手.详细文档及下载插件请移步这里. 基本使用 下面我们来实际操作,运用一下这个有爱的小插件. 需要做的事情非常简单,在页面上放一个DIV,id取名为‘star’或者什么的随你所以爱,用来显示我们的评分插件. 1 <div id="star"></div> 当然,记得把下载下来的jquery.raty.js或者jquery.raty.min.js放在项目文件夹当中.同时,由于是基于jQu