Rating system with stars

核能气质少年 提交于 2019-12-02 07:58:57

You can tweak your markup/CSS and achieve this by using the below approach (updated with some of your code), which I wrote some time back. Note that this also wraps your inputs within a more semantic fieldset structure, as well as better UI in terms of currently hovered / currently selected items. If part of a form, the relevant value will also be correctly submitted.

$('.rating input').change(
  function() {
    $('#choice').text(this.value + ' stars');
  }
)
.rating {
  float: left;
  border: none;
}
.rating:not(:checked) > input {
  position: absolute;
  top: -9999px;
  clip: rect(0, 0, 0, 0);
}
.rating:not(:checked) > label {
  float: right;
  width: 1em;
  padding: 0 .1em;
  overflow: hidden;
  white-space: nowrap;
  cursor: pointer;
  font-size: 200%;
  line-height: 1.2;
  color: #ddd;
}
.rating:not(:checked) > label:before {
  content: '★ ';
}
.rating > input:checked ~ label {
  color: #f70;
}
.rating:not(:checked) > label:hover,
.rating:not(:checked) > label:hover ~ label {
  color: gold;
}
.rating > input:checked + label:hover,
.rating > input:checked + label:hover ~ label,
.rating > input:checked ~ label:hover,
.rating > input:checked ~ label:hover ~ label,
.rating > label:hover ~ input:checked ~ label {
  color: #ea0;
}
.rating > label:active {
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset class="rating">
  <input type="radio" id="star5" name="rating" value="5" />
  <label for="star5">5 stars</label>
  <input type="radio" id="star4" name="rating" value="4" />
  <label for="star4">4 stars</label>
  <input type="radio" id="star3" name="rating" value="3" />
  <label for="star3">3 stars</label>
  <input type="radio" id="star2" name="rating" value="2" />
  <label for="star2">2 stars</label>
  <input type="radio" id="star1" name="rating" value="1" />
  <label for="star1">1 star</label>
</fieldset>

<div id="choice"></div>

I would suggest using this css based rating system i have built. It is very easy to understand and modify

HTML:

<formset id = "rating" class = "rating">
<input type = "radio" id = "r0"  name = "rating" checked = true style = "display:none;"> <label for = "r0" style = "display:none;"></label>
<input type = "radio" id = "r1"  name = "rating"> <label for = "r1"></label>
<input type = "radio" id = "r2"  name = "rating"> <label for = "r2"></label>
<input type = "radio" id = "r3"  name = "rating"> <label for = "r3"></label>
<input type = "radio" id = "r4"  name = "rating"> <label for = "r4"></label>
<input type = "radio" id = "r5"  name = "rating"> <label for = "r5"></label>
</formset>

CSS:

fieldset, label { margin: 0; padding: 0; }
.rating
{
  color: yellow;
}
formset > label, formset > input
{
  float: left;
}


formset > label::after
{
    content: '★ ';
    font-size: 50px;
    color: lightpink;
}

formset:hover > input:not(:checked) ~ label::after,
formset:not(:hover) > label::after,
formset:hover > input:checked ~ label::after
{
  color: orange;
}



formset:hover > input:checked ~ label:hover ~ label::after,
formset:hover > label:hover ~ label::after,
formset:not(:hover) > input:checked~label~label::after
{
  color: grey;
}

formset:hover > input:checked ~  label~ label::after,
formset:hover > label:hover ~ label::after
{
    color: lightpink;
}

formset:hover > label:hover~ input:checked~label~label::after
{
  color: grey;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!