如何防止所有单选按钮在评分系统中突出显示

时间:2019-05-12 18:23:45

标签: html css

我正在创建一个具有2个区域进行评分(2行星星)的星级评分系统。当我在第一排星星中选择一个评分时,它可以正常工作。但是,当我在第二行中选择时,无论我选择什么等级,所有五颗星都会突出显示。有人可以协助我解决CSS出现问题的地方吗?

body {
  padding: 50px;
}

.stars {
  background: url("stars.png") repeat-x 0 0;
  width: 150px;
  margin: 0 auto;
}

.stars1 {
  background: url("stars1.png") repeat-x 0 0;
  width: 150px;
  margin: 0 auto;
}

.stars input[type="radio"] {
  position: absolute;
  opacity: 0;
  filter: alpha(opacity=0);
}

.stars1 input[type="radio"] {
  position: absolute;
  opacity: 0;
  filter: alpha(opacity=0);
}

.stars input[type="radio"].star-5:checked~#span1 {
  width: 100%;
}

.stars1 input[type="radio"].stars-5:checked~#span2 {
  width: 100%;
}

.stars input[type="radio"].star-4:checked~#span1 {
  width: 80%;
}

.stars1 input[type="radio"].stars-4:checked~#span2 {
  width: 100%;
}

.stars input[type="radio"].star-3:checked~#span1 {
  width: 60%;
}

.stars1 input[type="radio"].stars-3:checked~#span2 {
  width: 100%;
}

.stars input[type="radio"].star-2:checked~#span1 {
  width: 40%;
}

.stars1 input[type="radio"].stars-2:checked~#span2 {
  width: 100%;
}

.stars input[type="radio"].star-1:checked~#span1 {
  width: 20%;
}

.stars1 input[type="radio"].stars-1:checked~#span2 {
  width: 100%;
}

.stars label {
  display: block;
  width: 30px;
  height: 30px;
  margin: 0 !important;
  padding: 0 !important;
  text-indent: -999em;
  float: left;
  position: relative;
  z-index: 10;
  background: transparent !important;
  cursor: pointer;
}

.stars1 label {
  display: block;
  width: 30px;
  height: 30px;
  margin: 0 !important;
  padding: 0 !important;
  text-indent: -999em;
  float: left;
  position: relative;
  z-index: 10;
  background: transparent !important;
  cursor: pointer;
}

.stars label:hover~#span1 {
  background-position: 0 -30px;
}

.stars1 label:hover~#span2 {
  background-position: 0 -30px;
}

.stars label.star-5:hover~#span1 {
  width: 100% !important;
}

.stars1 label.stars-5:hover~#span2 {
  width: 100% !important;
}

.stars label.star-4:hover~#span1 {
  width: 80% !important;
}

.stars1 label.stars-4:hover~#span2 {
  width: 80% !important;
}

.stars label.star-3:hover~#span1 {
  width: 60% !important;
}

.stars1 label.stars-3:hover~#span2 {
  width: 60% !important;
}

.stars label.star-2:hover~#span1 {
  width: 40% !important;
}

.stars1 label.stars-2:hover~#span2 {
  width: 40% !important;
}

.stars label.star-1:hover~#span1 {
  width: 20% !important;
}

.stars1 label.stars-1:hover~#span2 {
  width: 20% !important;
}

.stars #span1 {
  display: block;
  width: 0;
  position: relative;
  top: 0;
  left: 0;
  height: 30px;
  background: url("stars.png") repeat-x 0 -60px;
  -webkit-transition: -webkit-width 0.5s;
  -moz-transition: -moz-width 0.5s;
  -ms-transition: -ms-width 0.5s;
  -o-transition: -o-width 0.5s;
  transition: width 0.5s;
}

.stars1 #span2 {
  display: block;
  width: 0;
  position: relative;
  top: 0;
  left: 0;
  height: 30px;
  background: url("stars1.png") repeat-x 0 -60px;
  -webkit-transition: -webkit-width 0.5s;
  -moz-transition: -moz-width 0.5s;
  -ms-transition: -ms-width 0.5s;
  -o-transition: -o-width 0.5s;
  transition: width 0.5s;
}
<div class="stars">
  <input type="radio" name="star" class="star-1" id="star-1" />
  <label class="star-1" for="star-1">1</label>

  <input type="radio" name="star" class="star-2" id="star-2" />
  <label class="star-2" for="star-2">2</label>

  <input type="radio" name="star" class="star-3" id="star-3" />
  <label class="star-3" for="star-3">3</label>

  <input type="radio" name="star" class="star-4" id="star-4" />
  <label class="star-4" for="star-4">4</label>

  <input type="radio" name="star" class="star-5" id="star-5" />
  <label class="star-5" for="star-5">5</label>

  <span id="span1"></span>
</div>
<br>

<div class="stars1">
  <input type="radio" name="star1" class="stars-1" id="stars-1" />
  <label class="stars-1" for="stars-1">1</label>

  <input type="radio" name="star1" class="stars-2" id="stars-2" />
  <label class="stars-2" for="stars-2">2</label>

  <input type="radio" name="star1" class="stars-3" id="stars-3" />
  <label class="stars-3" for="stars-3">3</label>

  <input type="radio" name="star1" class="stars-4" id="stars-4" />
  <label class="stars-4" for="stars-4">4</label>

  <input type="radio" name="star1" class="stars-5" id="stars-5" />
  <label class="stars-5" for="stars-5">5</label>

  <span id="span2"></span>
</div>

1 个答案:

答案 0 :(得分:1)

对于所有star1选择器,您忘记为#span2提供不同的宽度,并且所有这些都设置为100%宽度。编辑您的CSS中的内容

.stars1 input[type="radio"].stars-5:checked ~ #span2 {
    width: 100%;
}
.stars1 input[type="radio"].stars-4:checked ~ #span2 {
    width: 80%;
}
.stars1 input[type="radio"].stars-3:checked ~ #span2 {
    width: 60%;
}
.stars1 input[type="radio"].stars-2:checked ~ #span2 {
    width: 40%;
}
.stars1 input[type="radio"].stars-1:checked ~ #span2 {
    width: 20%;
}