Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Star rating system hover

Currently I'm making a rating system for a webshop. Basically how I want it to work:

If visitor never rated before:

  • Visitor hovers over a star
  • The previous and current stars will be yellow, the next stars gray (done with class)
  • If the visitor leaves the hover, reset all stars to the old state
  • If the visitor clicks on a star, save it, calculate the next star values and update the array.

I'm using font awesome so I'm not using any images. The problem now is that if I hover over a star, it works, but if I want to move from star to star it glitches (because there's a little gap between the stars and it means it'll reset the stars first).

jsfiddle: https://jsfiddle.net/uappvz3y/

JS:

var current_star_statusses = [];

star_elements = $('.fa-star');

star_elements.each(function(i, elem)
{
    current_star_statusses.push($(elem).hasClass('yellow'));
});

star_elements.mouseenter(changeRatingStars);
star_elements.mouseleave(resetRatingStars);

/**
 * Changes the rating star colors when hovering over it.
 */
function changeRatingStars()
{
    // Current star hovered
    var star = $(this);

    // Removes all colors first from all stars
    $('.fa-star').removeClass('gray').removeClass('yellow');

    // Makes the current hovered star yellow
    star.addClass('yellow');

    // Makes the previous stars yellow and the next stars gray
    star.parent().prevAll().children('.fa-star').addClass('yellow');
    star.parent().nextAll().children('.fa-star').addClass('gray');
}

/**
 * Resets the rating star colors when not hovered anymore.
 */
function resetRatingStars()
{
    star_elements.each(function(i, elem)
    {
        $(elem).removeClass('yellow').removeClass('gray').addClass(current_star_statusses[i] ? 'yellow' : 'gray');
    });
}

HTML:

<ul class="list-inline rating-list">
    <li><i class="fa fa-star yellow"></i></li>
    <li><i class="fa fa-star yellow"></i></li>
    <li><i class="fa fa-star yellow"></i></li>
    <li><i class="fa fa-star yellow"></i></li>
    <li><i class="fa fa-star gray"></i></li>
</ul>

CSS:

.fa-star:before {
    content: "\f005";
}

.rating-list li i.yellow {
    color: #FFD700;
}

.rating-list li i.gray {
    color: #bbb;
}

.list-inline>li {
    display: inline-block;
    padding-right: 5px;
    padding-left: 5px;
}

.rating-list li {
    padding: 0px;
}
.fa {
    display: inline-block;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transform: translate(0, 0);
}

I know there are a lot of libraries that makes it easier but I'd like to keep it my own code if I can.

like image 334
Joshua Bakker Avatar asked Feb 24 '17 08:02

Joshua Bakker


1 Answers

You can make stars rating using pure CSS. Float stars to right, and apply hover effect for li that has padding.

.rating-list li {
  float: right;
  color: #ddd;
  padding: 10px 5px;
}

.rating-list li:hover,
.rating-list li:hover ~ li {
  color: #ffd700;
}

.rating-list {
  display: inline-block;
  list-style: none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<ul class="list-inline rating-list">
  <li><i class="fa fa-star" title="Rate 5"></i></li>
  <li><i class="fa fa-star" title="Rate 4"></i></li>
  <li><i class="fa fa-star" title="Rate 3"></i></li>
  <li><i class="fa fa-star" title="Rate 2"></i></li>
  <li><i class="fa fa-star" title="Rate 1"></i></li>
</ul>
like image 151
Justinas Avatar answered Sep 21 '22 01:09

Justinas