jQuery 星级插件

CSS:

.rating_ui {
  position: relative;
  display: inline-block;
  overflow: hidden;
  width: 100%;
  height: 12px;
  margin-bottom: 10px;
  background-color: whitesmoke;
  //  border-radius: 3px !important;
  -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
  z-index: 2;
}

.rating_ui > .rating {
  position: absolute;
  height: 100%;
  line-height: 12px;
  color: white;
  text-align: center;
  border-right: #cecece solid 2px;
}

.rating_ui.editable > .rating {
  cursor: pointer;
}

.rating_ui > .rating.rating_active {
  background-color: #ffcc00;
  -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
  -webkit-transition: width 0.6s ease;
  -o-transition: width 0.6s ease;
  transition: width 0.6s ease;
}

.rating_ui.editable > .rating:hover {
  background-color: #ffcc00;
  border-right: #d9534f solid 2px;
  -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
  -webkit-transition: width 0.6s ease;
  -o-transition: width 0.6s ease;
  transition: width 0.6s ease;
}

.rating_ui > .rating-1 {
  width: 20%;
  z-index: 50;
}

.rating_ui > .rating-2 {
  width: 40%;
  z-index: 40;
}

.rating_ui > .rating-3 {
  width: 60%;
  z-index: 30;
}

.rating_ui > .rating-4 {
  width: 80%;
  z-index: 20;
}

.rating_ui > .rating-5 {
  width: 100%;
  z-index: 10;
}

     <div class="rating_ui editable">
            <div class="rating rating-1 rating_active" title="1" data-rating="1"></div>
            <div class="rating rating-2" title="2" data-rating="2"></div>
            <div class="rating rating-3" title="3" data-rating="3"></div>
            <div class="rating rating-4" title="4" data-rating="4"></div>
            <div class="rating rating-5" title="5" data-rating="5"></div>
            <input type="hidden" class="level" name="level" value="2">
          </div>

 js插件:

(function ($, window) {
    $.fn.ratingStar = function (options) {
        function get_value($rating) {
            return window.parseInt($('.rating.rating_active', $rating).data('rating'));
        }

        function set_value(level, $rating) {
            $('input.level', $rating).val(level);
            $('.rating', $rating).removeClass('rating_active');
            $('.rating-' + level, $rating).addClass('rating_active');
        }

        function init_value($rating) {
            var level = window.parseInt($('input.level', $rating).val());
            set_value(level, $rating);
        }

        return this.each(function () {
            var $rating = $(this);
            init_value($rating);
            $rating.on('click', function (event) {
                if ($(this).hasClass('editable')) {
                    var $active_rating = $(event.target);
                    var level = window.parseInt($active_rating.data('rating'));
                    set_value(level, $(this));
                    return false;
                }
            });
        })
    }
})(jQuery, window);

相关推荐