CSS - 评级css系统

时间:2017-07-07 01:01:52

标签: html css

我在网上找到了一些评级系统css。 目前我能够将其付诸实施,但是当显示多个评级时会出现问题。

只有最后一个评级类正在运作。



<div class="rate">
  <input type="radio" id="star5" name="rate" value="5" />
  <label for="star5" title="text">5 stars</label>
  <input type="radio" id="star4" name="rate" value="4" checked/>
  <label for="star4" title="text">4 stars</label>
  <input type="radio" id="star3" name="rate" value="3" />
  <label for="star3" title="text">3 stars</label>
  <input type="radio" id="star2" name="rate" value="2" />
  <label for="star2" title="text">2 stars</label>
  <input type="radio" id="star1" name="rate" value="1" />
  <label for="star1" title="text">1 star</label>
</div>

<div class="rate">
  <input type="radio" id="star5" name="rate" value="5" checked/>
  <label for="star5" title="text">5 stars</label>
  <input type="radio" id="star4" name="rate" value="4" />
  <label for="star4" title="text">4 stars</label>
  <input type="radio" id="star3" name="rate" value="3" />
  <label for="star3" title="text">3 stars</label>
  <input type="radio" id="star2" name="rate" value="2" />
  <label for="star2" title="text">2 stars</label>
  <input type="radio" id="star1" name="rate" value="1" />
  <label for="star1" title="text">1 star</label>
</div>
&#13;
{{1}}
&#13;
&#13;
&#13;

只有最后一个div正常工作。

2 个答案:

答案 0 :(得分:2)

如果你有相同的名字&#34;所有单选按钮上的标记,即使它们位于2个不同的div中,它们也只会被视为一组单选按钮。

此外,在元素上添加重复的ID是不好的做法

要修复此问题,您应该使用这样的html https://jsfiddle.net/cornelraiu/70sk3ft6/1/

<div class="rate">
    <input type="radio" name="rate" value="5" />
    <label title="text">5 stars</label>
    <input type="radio" name="rate" value="4" checked/>                    
    <label title="text">4 stars</label>                                   
    <input type="radio" name="rate" value="3" />
    <label title="text">3 stars</label>                                   
    <input type="radio" name="rate" value="2" />                           
    <label title="text">2 stars</label>                                   
    <input type="radio" name="rate" value="1" />                           
    <label title="text">1 star</label>
</div>

<div class="rate">
    <input type="radio" name="rate1" value="5" checked/>
    <label title="text">5 stars</label>
    <input type="radio" name="rate1" value="4" />                    
    <label title="text">4 stars</label>                                   
    <input type="radio" name="rate1" value="3" />
    <label title="text">3 stars</label>                                   
    <input type="radio" name="rate1" value="2" />                           
    <label title="text">2 stars</label>                                   
    <input type="radio" name="rate1" value="1" />                           
    <label title="text">1 star</label>
</div>

答案 1 :(得分:1)

你有两个问题:

  1. 重复的ID。 Ids必须是独一无二的。
  2. 您的评分组名称相同。每个组都应具有不同的名称
  3. 修正一切正常。

    &#13;
    &#13;
    .rate {
        float: left;
        /*pointer-events: none;*/
    }
    
    .rate:not(:checked) > input {
        position: absolute;
        top: -9999px;
    }
    
    .rate:not(:checked) > label {
        float: right;
        width: 1em;
        overflow: hidden;
        white-space: nowrap;
        cursor: pointer;
        font-size: 12px;
        color: #ccc;
    }
    
    .rate:not(:checked) > label:before { content: '★ '; }
    
    .rate > input:checked ~ label { color: #ffc700; }
    
    .rate:not(:checked) > label:hover, .rate:not(:checked) > label:hover ~ label { color: #deb217; }
    
    .rate > input:checked + label:hover, .rate > input:checked + label:hover ~ label, .rate > input:checked ~ label:hover, .rate > input:checked ~ label:hover ~ label, .rate > label:hover ~ input:checked ~ label { color: #c59b08; }
    &#13;
    <div class="rate">
        <input type="radio" id="v1_star5" name="rate1" value="5" />
        <label for="v1_star5" title="text">5 stars</label>
        <input type="radio" id="v1_star4" name="rate1" value="4" checked/>                    
        <label for="v1_star4" title="text">4 stars</label>                                   
        <input type="radio" id="v1_star3" name="rate1" value="3" />
        <label for="v1_star3" title="text">3 stars</label>                                   
        <input type="radio" id="v1_star2" name="rate1" value="2" />                           
        <label for="v1_star2" title="text">2 stars</label>                                   
        <input type="radio" id="v1_star1" name="rate1" value="1" />                           
        <label for="v1_star1" title="text">1 star</label>
        </div>
    
       <div class="rate">
        <input type="radio" id="v2_star5" name="rate2" value="5" checked/>
        <label for="v2_star5" title="text">5 stars</label>
        <input type="radio" id="v2_star4" name="rate2" value="4" />                    
        <label for="v2_star4" title="text">4 stars</label>                                   
        <input type="radio" id="v2_star3" name="rate2" value="3" />
        <label for="v2_star3" title="text">3 stars</label>                                   
        <input type="radio" id="v2_star2" name="rate2" value="2" />                           
        <label for="v2_star2" title="text">2 stars</label>                                   
        <input type="radio" id="v2_star1" name="rate2" value="1" />                           
        <label for="v2_star1" title="text">1 star</label>
        </div>
    &#13;
    &#13;
    &#13;

    我已删除了pointer-events样式,因此您可以看到完整的结果。