使用带有Fontawesome的jQuery UI滑块更改星级

时间:2016-05-11 20:11:40

标签: jquery html css uislider rating-system

我想在滑块值更改时更改右上角的星形项目,例如,当滑块值为1时,应该有1个星,对于值2,应该有2个星,依此类推,最多5个星保持如果可能,相同的html结构。我保持slide: function() {}为空,因为我不知道如何在值更改时创建函数。

$(document).ready(function() {
  $('.slider-rating').slider({
    range: true,
    min: 1,
    max: 5,
    values: [3, 5],
    slide: function() {

    }
  });
});
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
@import
url('https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css');
@import 
url('https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css');

.control-group {
	margin-bottom: 10px;
  padding: 50px 20px;
}

.control-group .controls {
	margin: 5px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<div class="control-group">
  <div class="control-top clearfix">
    <div class="control-title pull-left">Ratings</div>
    <div class="control-percentage pull-right">
      <span class="slider-rating-label">
        <i class="fa fa-star"></i>
        <i class="fa fa-star"></i>
        <i class="fa fa-star"></i>
      </span>
    </div>
  </div>
  <div class="controls">
    <div class="slider slider-rating"></div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

添加了两颗星,然后我这样做了:

$(document).ready(function() {
  $('.slider-rating').slider({
    range: true,
    min: 1,
    max: 5,
    values: [3, 5],
    slide: function(x,y) {
        var stars = $('.slider-rating-label').find('i');
        //clear all
        $.each(stars, function (index, value) {
            $(value).removeClass().addClass('fa fa-star-o');
        });
        //fill by slider value
        for (var i = 0; i < y.value; i++) {
            $(stars[i]).removeClass().addClass('fa fa-star');
        }
    }
  });
});