星级评级单选按钮css

时间:2014-03-21 21:36:29

标签: html css

在这里:http://jsfiddle.net/ghvNn/261/我创造了两星评级。来自此网页http://rog.ie/blog/css-star-rater。但是这个代码只适用于5星,但我想拥有它的UNIVERSAL CODE(3,5,7,10)星。我该怎么办?

<span class="star-rating">
  <input type="radio" name="rating" value="1"><i></i>
  <input type="radio" name="rating" value="2"><i></i>
  <input type="radio" name="rating" value="3"><i></i>
  <input type="radio" name="rating" value="4"><i></i>
  <input type="radio" name="rating" value="5"><i></i>
</span>
<br><br>
<span class="star-rating">      
  <input type="radio" name="rating" value="1"><i></i>
  <input type="radio" name="rating" value="2"><i></i>
  <input type="radio" name="rating" value="3"><i></i>
</span>

2 个答案:

答案 0 :(得分:4)

将额外的类添加到span.star-rating以分隔具有不同计数的星形样式。例如:

<span class="star-rating star-5">

然后删除这种风格:

.star-rating {width: 250px;}
.star-rating i {width: 20%;}
.star-rating input {width: 20%;}

最后你可以添加这个样式:

.star-rating.star-5 {width: 250px;}
.star-rating.star-5 input,
.star-rating.star-5 i {width: 20%;}
.star-rating.star-5 i ~ i {width: 40%;}
.star-rating.star-5 i ~ i ~ i {width: 60%;}
.star-rating.star-5 i ~ i ~ i ~ i {width: 80%;}
.star-rating.star-5 i ~ i ~ i ~ i ~i {width: 100%;}

.star-rating.star-3 {width: 150px;}
.star-rating.star-3 input,
.star-rating.star-3 i {width: 33.33%;}
.star-rating.star-3 i ~ i {width: 66.66%;}
.star-rating.star-3 i ~ i ~ i {width: 100%;}

DEMO: JSFiddle

答案 1 :(得分:1)

Demo JSFiddle

这里有10星的代码

HTML:

 <span class="star-rating">
  <input type="radio" name="rating" value="1"><i></i>
  <input type="radio" name="rating" value="2"><i></i>
  <input type="radio" name="rating" value="3"><i></i>
  <input type="radio" name="rating" value="4"><i></i>
  <input type="radio" name="rating" value="5"><i></i>
  <input type="radio" name="rating" value="6"><i></i>
  <input type="radio" name="rating" value="7"><i></i>
  <input type="radio" name="rating" value="8"><i></i>
  <input type="radio" name="rating" value="9"><i></i>
  <input type="radio" name="rating" value="10"><i></i>
</span>

CSS:

.star-rating {
  font-size: 0;
  white-space: nowrap;
  display: inline-block;
  width: 500px;
  height: 50px;
  overflow: hidden;
  position: relative;
  background:     url('data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iMjBweCIgaGVpZ2h0PSIyMHB4IiB2aWV3Qm94PSIwIDAgMjAgMjAiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDIwIDIwIiB4bWw6c3BhY2U9InByZXNlcnZlIj48cG9seWdvbiBmaWxsPSIjREREREREIiBwb2ludHM9IjEwLDAgMTMuMDksNi41ODMgMjAsNy42MzkgMTUsMTIuNzY0IDE2LjE4LDIwIDEwLDE2LjU4MyAzLjgyLDIwIDUsMTIuNzY0IDAsNy42MzkgNi45MSw2LjU4MyAiLz48L3N2Zz4=');
  background-size: contain;
}
.star-rating i {
  opacity: 0;
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 10%;
  z-index: 1;
  background:     url('data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iMjBweCIgaGVpZ2h0PSIyMHB4IiB2aWV3Qm94PSIwIDAgMjAgMjAiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDIwIDIwIiB4bWw6c3BhY2U9InByZXNlcnZlIj48cG9seWdvbiBmaWxsPSIjRkZERjg4IiBwb2ludHM9IjEwLDAgMTMuMDksNi41ODMgMjAsNy42MzkgMTUsMTIuNzY0IDE2LjE4LDIwIDEwLDE2LjU4MyAzLjgyLDIwIDUsMTIuNzY0IDAsNy42MzkgNi45MSw2LjU4MyAiLz48L3N2Zz4=');
  background-size: contain;
}
.star-rating input {
  -moz-appearance: none;
  -webkit-appearance: none;
  opacity: 0;
  display: inline-block;
  width: 10%;
  height: 100%;
  margin: 0;
  padding: 0;
  z-index: 2;
  position: relative;
}
.star-rating input:hover + i,
.star-rating input:checked + i {
  opacity: 1;
}
.star-rating i ~ i {
  width: 20%;
}
.star-rating i ~ i ~ i {
  width: 30%;
}
.star-rating i ~ i ~ i ~ i {
  width: 40%;
}
.star-rating i ~ i ~ i ~ i ~ i {
  width: 50%;
}
.star-rating i ~ i ~ i ~ i ~ i ~ i {
  width: 60%;
}
.star-rating i ~ i ~ i ~ i ~ i ~ i ~ i {
  width: 70%;
}
.star-rating i ~ i ~ i ~ i ~ i ~ i ~ i ~ i {
  width: 80%;
}
.star-rating i ~ i ~ i ~ i ~ i ~ i ~ i ~ i ~ i {
  width: 90%;
}
.star-rating i ~ i ~ i ~ i ~ i ~ i ~ i ~ i ~ i ~ i {
  width: 100%;
}
::after,
::before {
  height: 100%;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  text-align: center;
  vertical-align: middle;
}

正如您所看到的,您需要做的就是在HTML中添加输入元素,然后在CSS中更改.star-rating元素的宽度,并为每个'i'元素添加适当的宽度值百分比。