javascript – 星级评分系统悬停

目前我正在为网上商店制作评级系统.基本上我希望它如何工作:

如果访客从未评价过:

>游客在一颗星上盘旋
>以前和当前的明星将是黄色,接下来的明星是灰色的(完成课程)
>如果访客离开悬停,则将所有星星重置为旧状态
>如果访问者点击星形,保存它,计算下一个星形值并更新阵列.

我使用字体很棒,所以我没有使用任何图像.现在的问题是,如果我将鼠标悬停在一颗恒星上,它会起作用,但是如果我想从一颗恒星移动到另一颗恒星,它就会出现故障(因为恒星之间有一点间隙,这意味着它会首先重置恒星).

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);
}

我知道有很多库可以让它变得更容易但是如果可以的话我想保留它自己的代码.

解决方法:

我改变了这4行代码.

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

star_elements.find(".fa-star").each(function(i, elem) {
  current_star_statusses.push($(elem).hasClass('yellow'));
});

star_elements.find(".fa-star").mouseenter(changeRatingStars);
star_elements.find(".fa-star").mouseleave(resetRatingStars);

所以现在star_element就是李.

此外,如果你pref jsfiddle,这是一个link

var current_star_statusses = [];

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

star_elements.find(".fa-star").each(function(i, elem) {
  current_star_statusses.push($(elem).hasClass('yellow'));
});

star_elements.find(".fa-star").mouseenter(changeRatingStars);
star_elements.find(".fa-star").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');
  });
}
.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);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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 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选择器:伪类(图文详解)


下一篇:CSS学习笔记