如何使用CSS和jQuery创建具有span或ul li的简单星级? [英] How do I create a simple star rating with span or ul li using CSS and jQuery?

查看:90
本文介绍了如何使用CSS和jQuery创建具有span或ul li的简单星级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了输入 type =radio的星级评分示例。

I found an example of star rating with input of type="radio".

这太棒了,但我想要更简单的东西。而不是输入 type =radio,请使用星级评分< span> 演示(演示)示例

It's fantastic, but I'd like something simpler. Instead of input of type="radio", use star rating with <span> example in demonstration (demo).

以下代码

.holder {
  height: 1%;
  overflow: hidden;
  position: relative;
  left: 0;
}

.left {
  float: left;
}

.rating {
  border: none;
  float: none;
  display: block;
  left: 0;
}

.rating>input {
  display: none;
}

.rating>label:before {
  margin: 5px;
  font-size: 1.25em;
  font-family: FontAwesome;
  display: inline-block;
  content: "\f005";
}

.rating>.half:before {
  content: "\f089";
  position: absolute;
}

.rating>label {
  color: #ddd;
  float: right;
}


/*    Styles hover mouse stars.       */

.rating>input:checked~label,

/* Show gold star when clicked */

.rating:not(:checked)>label:hover,

/* Hover current star */

.rating:not(:checked)>label:hover~label {
  color: #FFD700;
}


/* Hover previous stars in list */

.rating>input:checked+label:hover,

/* Hover current star when changing rating */

.rating>input:checked~label:hover,
.rating>label:hover~input:checked~label,

/* Lighten current selection */

.rating>input:checked~label:hover~label {
  color: #FFED85;
}

<!DOCTYPE html>
<html lang="" es>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <!-- FontAwsome Bootstrap -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
  <script>
    $(document).ready(function() {
      $(document).on('submit', '#frm-rating', function() {
        //Get form data.
        var data = $(this).serialize();
        //Ajax
        $.ajax({
          type: 'POST',
          url: 'rating.php',
          data: data,
          success: function(data) {
            $("#result-rating").html(data);
          }
        });
        return false;
      });
    });
  </script>
</head>

<body>
  <form id="frm-rating" method="POST">
    <div class="holder">
      <div class="left">
        <fieldset class="rating">
          <input type="radio" id="star5" name="rating" value="5" /><label class="full" for="star5" title="Excellent - 5 stars"></label>
          <input type="radio" id="star4half" name="rating" value="4.5" /><label class="half" for="star4half" title="very good - 4.5 stars"></label>
          <input type="radio" id="star4" name="rating" value="4" /><label class="full" for="star4" title="All right - 4 stars"></label>
          <input type="radio" id="star3half" name="rating" value="3.5" /><label class="half" for="star3half" title="All right - 3.5 stars"></label>
          <input type="radio" id="star3" name="rating" value="3" /><label class="full" for="star3" title="Regular - 3 stars"></label>
          <input type="radio" id="star2half" name="rating" value="2.5" /><label class="half" for="star2half" title="Regulate the evil - 2.5 stars"></label>
          <input type="radio" id="star2" name="rating" value="2" /><label class="full" for="star2" title="wrong - 2 stars"></label>
          <input type="radio" id="star1half" name="rating" value="1.5" /><label class="half" for="star1half" title="Very bad - 1.5 stars"></label>
          <input type="radio" id="star1" name="rating" value="1" /><label class="full" for="star1" title="Painful - 1 star"></label>
          <input type="radio" id="starhalf" name="rating" value="0.5" /><label class="half" for="starhalf" title="Waste of time - 0.5 stars"></label>
        </fieldset>
      </div>
    </div>

    <textarea name="opinion" placeholder="Write a comment..."></textarea>

    <!-- We use the user's session to get their ID in value -->
    <input type="hidden" name="user" value="1" />

    <button type="submit">Submit Review</button>
  </form>

  <div id="result-rating">
    <!-- Respuesta Ajax. -->
  </div>

</body>

</html>

如何在不使用框架作为Bootstrap或其他框架的情况下仅使用CSS和jQuery在此代码中创建相同特征的星星?

How can I create stars of the same characteristics in this code using only CSS and jQuery without using frameworks as Bootstrap or others?

<div id="rating" class="rating">
  <span class="stars"></span>
  <span class="stars"></span>
  <span class="stars"></span>
  <span class="stars"></span>
  <span class="stars"></span>
</div>

或者此代码:

<ul class="star-rating">
  <li class="star-icon">&#9734;</li>
  <li class="star-icon">&#9734;</li>
  <li class="star-icon">&#9734;</li>
  <li class="star-icon half">&#9734;</li>
  <li class="star-icon full">&#9734;</li>
</ul>

通过jQuery,获取所选值并将它们发送到数据库。

And by means of jQuery, get the selected values and send them to the database.

$(document).ready(function() {
  $(document).on('submit', '#frm-rating', function() {
    //Obtenemos datos form.
    var data = $(this).serialize();
    //Ajax
    $.ajax({
      type: 'POST',
      url: 'rating.php',
      data: data,
      success: function(data) {
        $("#result-rating").html(data);
      }
    });
    return false;
  });
});

目标是优化更简单的代码。

The goal is to have simpler code optimized.

示例:

.star-icon {
    color: #ddd;
    font-size: 2em;
    position: relative;
    display: inline;
    direction: rtl;
    unicode-bidi: bidi-override;
}

.star-icon:before{
    content: '\2605';
    position: absolute;
    left: 0;
}

.star-icon.full:before {
    color: #fde16d;
    content: '\2605';
    position: absolute;
    left: 0;
}

.star-icon.half:before {
    color: #fde16d;
    content: '\2605';
    position: absolute;
    left: 0;
    width: 50%;
    overflow: hidden;
    direction: ltr;
}

.star-icon.half:after {
    content: '\2605';
    position: absolute;
    right: 0;
    width: 50%;
    overflow: hidden;
}

.star-rating > li:hover,
.star-rating > li:hover ~li:before,
.star-rating > li:hover ~li:after
{
    width: 100%;
    color: #fde16d;
    cursor: pointer;
}

<ul class="star-rating">
  <li class="star-icon">&#9734;</li>
  <li class="star-icon">&#9734;</li>
  <li class="star-icon">&#9734;</li>
  <li class="star-icon half">&#9734;</li>
  <li class="star-icon full">&#9734;</li>
</ul>

现在要知道用户通过PHP和jQuery选择了什么评级。

Now to know what rating the user has chosen through PHP and jQuery.

更动态。

<?php
    function calculate_stars($max, $rating){
        $full_stars=floor($rating);
        $half_stars = ceil($rating-$full_stars);
        $gray_stars = $max-($full_stars + $half_stars);
        return array ($full_stars, $half_stars, $gray_stars);
    }

    function display_star($rating){
        $output="";
        $number_stars = calculate_stars(5,$rating);
        $full = $number_stars[0];
        $half = $number_stars[1];
        $gray = $number_stars[2];
        $output ='<ul class="star-rating">';
        if($gray)
            for ($i=0;$i<$gray;$i++)
            {
                $output .= '<li class="star-icon">&#9734;</li>';
            }

        if($half){
            $output .= '<li class="star-icon half">&#9734;</li>';
        }

        if($full){
            for($i=0; $i<$full;$i++)
            {
                $output .= '<li class="star-icon full">&#9734;</li>';
            }
        }

        $output .='</ul>';
        return $output;
    }

    echo display_star(4.3);

    exit;
?>

Source 观察解释主题的前3个视频。

Source observe the first 3 videos explaining the theme.

我不知道想用JavaScript;我希望它在jQuery中。

I do not want to use JavaScript; I want it in jQuery.

推荐答案

HTML标记:

<ul class="c-rating">
    <li class="c-rating__item rating is-active left" data-index="0"></li>
    <li class="c-rating__item" data-index="1"></li>
    <li class="c-rating__item left" data-index="2"></li>
    <li class="c-rating__item" data-index="3"></li>
    <li class="c-rating__item left" data-index="4"></li>
    <li class="c-rating__item" data-index="5"></li>
    <li class="c-rating__item left" data-index="6"></li>
    <li class="c-rating__item" data-index="7"></li>
    <li class="c-rating__item left" data-index="8"></li>
    <li class="c-rating__item" data-index="9"></li>
</ul>
<input type="hidden" class="rank" />

我为此功能创建了一个自定义jQuery插件,您需要传递 childClass 用于子选择器, inputClass 用于输入标签选择器。

I have created a custom jQuery plugin for this functionality where you need to pass childClass for child selector and inputClass for input tag selector.

var activeClassCount = 1;
$.fn.customStarRatings = function(obj) {
    var wrapper = this;
    var stars = obj.childClass;
    var input = obj.inputClass;
    $(wrapper).hover(function() {
        activeClassCount = $(this).find('.rating').index() + 1;
    }, function() {
        var $this = $(this);
        $this.find(stars).slice(1, activeClassCount).addClass('is-active');
        $this.find(stars).slice(activeClassCount, 10).not('.rating').removeClass('is-active');
        // console.log($this.find(stars).slice(activeClassCount, 4).not('.rating'))
    });
    $(stars).hover(function() {
        $(this).prevAll(stars).add($(this)).addClass('is-active');
        $(this).nextAll(stars).removeClass('is-active');
    });
    $(stars).click(function(event) {
        $(".rating").removeClass("rating");
        $(this).addClass('rating');
        activeClassCount = $(this).index() + 1;
        $(this).prevAll(stars).addClass('is-active');
        if (input) {
            $(input).val(($(this).index() + 1)/2);
            console.log($(input).val(), activeClassCount)
        }
    });
}

然后你必须在DOM准备就绪时调用这个jQuery函数。 / p>

Then you have to simply call this jQuery function when the DOM gets ready.

$(".c-rating").customStarRatings({
    childClass: ".c-rating__item",
    inputClass: ".rank"
});

PS HTML标记不仅限于使用< ul> < li> 仅限代码。您需要做的就是在评级和输入元素的包装标签上调用此自定义函数。

PS The HTML markup is not confined to usage of <ul> and <li> tags only. All you need to do is call this custom function on the wrapper tag of ratings and input element.

CSS编辑:

.c-rating {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
}

.c-rating__item {
    -webkit-box-flex: 0;
     -webkit-flex: 0 0 12px;
    -ms-flex: 0 0 12px;
    flex: 0 0 12px;
    height: 24px;
    background-position: -12px 0px;
    background-image: url(stars.svg);
    cursor: pointer;
}
.c-rating__item.left {
    background-position: 0px 0px;
}
.c-rating__item.is-active,
.c-rating__item: hover {
    background-position: -36px 0;
}
.c-rating__item.left.is-active,
.c-rating__item.left: hover {
    background-position: -24px 0;
}

对此函数的jQuery调用保持不变。检查工作小提琴此处

The jQuery call to this function remains the same. Check the working fiddle here.

这篇关于如何使用CSS和jQuery创建具有span或ul li的简单星级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆