Change star rating on hover

后端 未结 4 1630
南旧
南旧 2021-01-08 01:09

I use this code to display stars:

4条回答
  •  情歌与酒
    2021-01-08 01:47

    This can almost work with pure CSS. It does need one line of JavaScript to set the initial value though:

    document.getElementById('Degelijkheid-1-5').checked = true;
    .rating input {
        display: none;
    }
    .rating label:before {
        margin: 5px;
        font-size: 1.25em;
        font-family: FontAwesome;
        display: inline-block;
        content:"\f005";
    }
    li {
        list-style:none;
    }
    
    .ratingSelector input + label {
        color: #FFD700;
    }
    .ratingSelector input:checked ~ input:not(:checked) ~ label {
        color: #ffffd;
    }
    .ratingSelector input:checked ~ input:not(:checked) + label:hover ~ label {
        color: #ffffd;
    }
    .ratingSelector:hover input + label,
    .ratingSelector:hover input:checked + label {
        color: #FFED85;
    }
    .ratingSelector:hover input:checked ~ input:not(:checked) ~ label:hover,
    .ratingSelector:hover input:checked ~ input:not(:checked) + label {
        color: #FFD700;
    }
    .ratingSelector:hover input:checked ~ input:not(:checked) ~ label:hover ~ label {
        color: #ffffd !important;
    }
    .ratingSelector input + label:hover ~ label {
        color: #ffffd !important;
    }

    EDIT

    To use JS (to re-order the items):

    var objGroup = document.getElementsByClassName('ratingSelector');
    for (var i = 0; i < objGroup.length; i++) {
      var objRadio = [].slice.call(objGroup[i].getElementsByClassName('full')); // Create an array of items
      for (var j = objRadio.length; j--;) { // Loop through the array backwards
        var checkbox = document.getElementById(objRadio[j].getAttribute('for'));
        objGroup[i].appendChild(checkbox);
        objGroup[i].appendChild(objRadio[j]);
      }
    }
    .rating {
      direction: rtl;
      text-align: left;
    }
    .rating input {
      display: none;
    }
    .rating label:before {
      margin: 5px;
      font-size: 1.25em;
      font-family: FontAwesome;
      display: inline-block;
      content: "\f005";
    }
    li {
      list-style: none;
    }
    .rating label {
      color: #ffffd;
    }
    /***** CSS to Highlight Stars on Hover *****/
    
    .ratingSelector input:checked ~ label,
    /* show gold star when clicked */
    
    .ratingSelector label:hover,
    /* hover current star */
    
    .ratingSelector label:hover ~ label {
      color: #FFD700;
    }
    /* hover previous stars in list */
    
    .ratingSelector input:checked + label:hover,
    /* hover current star when changing rating */
    
    .ratingSelector input:checked ~ label:hover,
    .ratingSelector label:hover ~ input:checked ~ label,
    /* lighten current selection */
    
    .ratingSelector input:checked ~ label:hover ~ label {
      color: #FFED85;
    }

提交回复
热议问题