项目作者: fredolss

项目描述 :
Star rating widget for the browser. Unlimited number of stars. No dependencies. No Jquery required.
高级语言: JavaScript
项目地址: git://github.com/fredolss/rater-js.git
创建时间: 2017-05-07T13:33:27Z
项目社区:https://github.com/fredolss/rater-js

开源协议:MIT License

下载


Rater Js

rater-js Logo
Donate
NPM version
License
Downloads
Build Status

rater-js is a star rating widget for the browser.

Main features:

  • Unlimited number of stars.
  • Svg as background image makes it look good in any size.
  • Custom css. Use your own image as star.
  • RTL support.
  • Touch support.

Try Rater JS Demo →

Installation

  1. npm install rater-js --save

Usage

rater-js can be used with amd, commonjs or without any module loader using global scope.

In your html create an element that acts as the placeholder for the widget.

  1. <div id="rater"></div>

Global scope

Directly reference the js from the module

  1. <!--Add js before end body tag-->
  2. <script src="node-modules/rater-js/index.js"></script>

The widget will be available globally as “raterJs” on the window object.

Node/Browserify

Just require the module.

  1. var rater = require("rater-js");

Lastly we can use the widget like this:

  1. var myRater = rater({element: document.querySelector("#rater"), rateCallback: function rateCallback(rating, done) {
  2. //make async call to server however you want
  3. //in this example we have a 'service' that rate and returns the average rating
  4. myDataService.rate(rate).then(function(avgRating) {
  5. //update the avarage rating with the one we get from the server
  6. myRater.setRating(avgRating);
  7. //we could disable the rater to prevent another rating
  8. //if we dont want the user to be able to change their mind
  9. myRater.disable();
  10. //dont forget to call done
  11. done();
  12. }, function(error) {
  13. //handle the error
  14. //dont forget to call done
  15. done();
  16. });
  17. }});

Css will be injected at runtime, but you can override the css to get the look you want.

  1. //change the whole image used as the star. Make sure to set starSize in options if not 16px.
  2. //first image is for the 'off' mode
  3. .star-rating {
  4. background: url("myStar_off.svg") !important;
  5. }
  6. //add style for 'on' mode
  7. .star-rating .star-value{
  8. background: url("myStar_on.svg") !important;
  9. }

Configuration

Property Description
element HtmlElement. Required.
rateCallback Function. Triggered when star i clicked.
max Number. Number of stars to show.
showToolTip true/false. If set to true, show tooltip when hover the stars.
starSize Number. Width and height of the star image.
disableText Text to show when disabled.
ratingText Text to show when hover over stars. Text {rating} {maxRating}.
isBusyText Displayed while user is rating but done not called yet.
readOnly true/false. If set to true, will disable the rater.
step Number. Set a precision between 0 and 1 for the rating.
reverse true/false. If set to true, the ratings will be reversed.

Methods/Properties

  1. disable(): //Disable the widget
  2. enable(): //Enable the widget
  3. setRating(rating:number): //Set the rating
  4. getRating(): //Get the average rating
  5. dispose(); //Removes event handlers
  6. clear(); //Clears the rating
  7. element; //Get the element used by rater js