A zero-dependency library that transforms a select with numerical-range values (i.e. 1-5) into a dynamic star rating element.
A zero-dependency library that transforms a select with numerical-range values (i.e. 1-5) into a dynamic star rating element.
For production, use the files from the dist/
folder.
npm i star-rating.js
Your SELECT option fields must have numerical values.
<link href="css/star-rating.css" rel="stylesheet">
<select class="star-rating">
<option value="">Select a rating</option>
<option value="5">Excellent</option>
<option value="4">Very Good</option>
<option value="3">Average</option>
<option value="2">Poor</option>
<option value="1">Terrible</option>
</select>
<script src="js/star-rating.min.js"></script>
<script>
var stars = new StarRating('.star-rating');
</script>
To rebuild all star rating controls (e.g. after form fields have changed with ajax):
stars.rebuild();
To fully remove all star rating controls, including all attached Event Listeners:
stars.destroy();
Here are the default options
{
classNames: {
active: 'gl-active',
base: 'gl-star-rating',
selected: 'gl-selected',
},
clearable: true,
maxStars: 10,
stars: null,
tooltip: 'Select a Rating',
}
Type: String
The classname to use for the active (hovered or value <= of the selected value) state of a star.
Type: String
The classname to use for the base element that wraps the star rating.
Type: String
The classname to use for the selected state of a star.
Type: Boolean
Whether or not the star rating can be cleared by clicking on an already selected star.
Type: Integer
The maximum number of stars allowed in a star rating.
Type: Function
This can be used to add a SVG image to each star value instead of using the background images in the CSS.
Type: String|False
The placeholder text for the rating tooltip, or false
to disable the tooltip.
npm install
npm run build
The compiled files will be saved in the dist/
folder.
Following are the default CSS variable values for Star Rating:
:root {
--gl-star-color: #fdd835; /* if using SVG images */
--gl-star-color-inactive: #dcdce6; /* if using SVG images */
--gl-star-empty: url(../img/star-empty.svg); /* if using background images */
--gl-star-full: url(../img/star-full.svg); /* if using background images */
--gl-star-size: 24px;
--gl-tooltip-border-radius: 4px;
--gl-tooltip-font-size: 0.875rem;
--gl-tooltip-font-weight: 400;
--gl-tooltip-line-height: 1;
--gl-tooltip-margin: 12px;
--gl-tooltip-padding: .5em 1em;
--gl-tooltip-size: 6px;
}
To override any values with your own, simply import the CSS file into your project, then enter new CSS variable values after the import.
@import 'star-rating';
:root {
--gl-star-size: 32px;
}
Sometimes an existing stylesheet rules will override the default CSS styles for Star Ratings. To solve this problem, you can use the postcss-selector-namespace plugin in your PostCSS build on the CSS file before combining with your main stylesheet. This namespace value should be a high priority/specificity property such as an id attribute or similar.
If you need to use the Star Rating library in a unsupported browser (i.e. Internet Explorer), use the Polyfill service.
All changes should be committed to the files in src/
.
v4.0.2 - [2021-01-23]
'function' !== typeof options.stars
)v4.0.1 - [2021-01-22]
v4.0.0 - [2021-01-22]
stars
option which allows you to use custom SVG images for each starclassname
option with the classNames
optioninitialText
with the tooltip
optionv3.4.0 - [2020-10-18]
v3.2.0 - [2020-07-13]
v3.1.8 - [2020-06-29]
v3.1.5 - [2019-11-01]
v3.1.4 - [2019-01-28]
v3.1.3 - [2019-01-27]
v3.1.2 - [2019-01-07]
v3.1.1 - [2018-07-27]
v3.1.0 - [2018-07-24]
star-filled
SCSS map option to star-full
star-empty
, star-full
, and star-half
SCSS map options to url(...)
. This allows one to use none
as the value of background-image
.v3.0.0 - [2018-07-24]
onClick
option (listen for the change
event instead)v2.3.1 - [2018-07-22]
v2.3.0 - [2018-07-20]
$star-rating[parent]
SCSS optionv2.2.2 - [2018-07-16]
v2.2.1 - [2018-07-13]
v2.2.0 - [2018-07-09]
classname
option$star-rating[base-classname]
SCSS optionv2.1.1 - [2018-05-25]
v2.1.0 - [2018-05-11]
v2.0.0 - [2018-05-02]
v1.3.3 - [2017-04-11]
v1.3.1 - [2016-12-22]
v1.3.0 - [2016-10-10]
clickFn
to onClick
which now passes the select HTMLElement as the argumentv1.2.2 - [2016-10-10]
clearable
option is falsev1.2.1 - [2016-10-09]
v1.2.0 - [2016-10-09]
v1.1.0 - [2016-10-06]
showText
optionv1.0.1 - [2016-10-06]
v1.0.0 - [2016-10-06]
Author: pryley
Demo: https://pryley.github.io/star-rating.js/
Source Code: https://github.com/pryley/star-rating.js
JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....
The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview
JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.
Introduction With Basic JavaScript - Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world.
The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.