Improving site hover scheme

0

I'm trying to develop an evaluation scheme for files that are hosted on my client's website. Each file gains beyond the access link a DIV with the five rating options, which appear as gray stars. Here is the code:

<div style="margin-top: -2px">
    <?php for($s=1;$s<=5;$s++){ ?>
        <img id="Estrela<?php echo $s; ?>" class="starClick" onclick="javascript:vota(<?php echo $s; ?>)" src="http://meusite.com.br/star2.png"<?phpif($s==1){echo"style='margin-left: 5px'"; } ?> />
    <?php } ?>
</div>

And the Javascript code, when passing by a star it changes color. And by clicking, it tells you which of the five options you voted for:

<script type="text/javascript">
$('.starClick').hover(function() {
    $(this).attr('src', 'http://meusite.com.br/star1.png');
}, function() {
    $(this).attr('src', 'http://meusite.com.br/star2.png');
});

function vota(val){
    if(val == '1'){
        alert('Você votou HORRÍVEL');
    }
    else if(val == '2'){
        alert('Você votou RUIM');
    }
    else if(val == '3'){
        alert('Você votou MÉDIO');
    }
    else if(val == '4'){
        alert('Você votou BOM');
    }
    else if(val == '5'){
        alert('Você votou ÓTIMO');
    }
}
</script>

Is there any practical way to improve the hover function in this case? It would look something like this:

  • Go through Star 5 and all of them change color.
  • Go through star 4 and only stars 1, 2, 3 and 4 change color.
  • Go through star 3 and only stars 1, 2 and 3 change color.
  • Go through star 2 and only stars 1 and 2 change color.
  • Go through star 1 and it only changes color.

Because at the moment, only the respective star changes color as it passes through it. Anyone know?

    
asked by anonymous 22.03.2017 / 18:32

2 answers

1

Huum .. I understand good could use ajax this way ...

<script>
  $('input[type="radio"]').change(function (){
    //aqui pega o valor do voto
    var voto = $(this).val();
    //aqui pega o id do item (no caso id do arquivo a ser votado)
    var id = $(this.id);
    // pagina que vai realizar o insert
    $.post('pagina.php',{
      voto: star,
      id: id
    },function(data) {
        // retorna resultado do voto
        $('.star').html(data);
    });
  });
</script>
    
22.03.2017 / 19:31
1

Hello, well I've done a pretty similar script I think should help ...

    
    /* Estilo das estrelas quando elas estão "ativadas" checked */
    .radio-1:checked ~ .star-item-1:before,
    .radio-1:focus ~ .star-item-1:before, .radio-2:checked ~ .star-item-1:before,
    .radio-2:focus ~ .star-item-1:before, .radio-2:checked ~ .star-item-2:before,
    .radio-2:focus ~ .star-item-2:before, .radio-3:checked ~ .star-item-1:before,
    .radio-3:focus ~ .star-item-1:before, .radio-3:checked ~ .star-item-2:before,
    .radio-3:focus ~ .star-item-2:before, .radio-3:checked ~ .star-item-3:before,
    .radio-3:focus ~ .star-item-3:before, .radio-4:checked ~ .star-item-1:before,
    .radio-4:focus ~ .star-item-1:before, .radio-4:checked ~ .star-item-2:before,
    .radio-4:focus ~ .star-item-2:before, .radio-4:checked ~ .star-item-3:before,
    .radio-4:focus ~ .star-item-3:before, .radio-4:checked ~ .star-item-4:before,
    .radio-4:focus ~ .star-item-4:before, .radio-5:checked ~ .star-item-1:before,
    .radio-5:focus ~ .star-item-1:before, .radio-5:checked ~ .star-item-2:before,
    .radio-5:focus ~ .star-item-2:before, .radio-5:checked ~ .star-item-3:before,
    .radio-5:focus ~ .star-item-3:before, .radio-5:checked ~ .star-item-4:before,
    .radio-5:focus ~ .star-item-4:before, .radio-5:checked ~ .star-item-5:before,
    .radio-5:focus ~ .star-item-5:before, .star-item:hover:before, .star:hover .star-item:before {
      color: #ffca00;
    }
    
    /* Estilo das estrelas quando elas estão "desativadas" unchecked */
    .star-item:before, .star-item:hover ~ .star-item:before {
      color: #CCC;
    }
    
    .star-item {
      font: "0/0" a;
      color: transparent;
      text-shadow: none;
      background-color: transparent;
      border: 0;
    }
    
    /* Esconde radio */
    .radio {
      position: absolute;
      top: -999999em;
      left: auto;
      width: 1px;
      height: 1px;
      overflow: hidden;
    }
    
    /* Escreve os seletores das estrelas para marcalas como "ativas" checked, quando um input radio 3 está "ativo" checked, as estrelas 3, 2 e 1 devem aparecer "ativas", esse loop escreve essas classes para nós. */
    .star {
      display: -webkit-inline-box;
      display: -webkit-inline-flex;
      display: -ms-inline-flexbox;
      display: inline-flex;
      -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
      flex-wrap: wrap;
      pointer-events: none;
    }
    
    .star-item {
      cursor: pointer;
      display: inline-block;
      pointer-events: initial;
      width: 1em;
      height: 1em;
      overflow: hidden;
      line-height: 100%;
      font-size:28px;
    }
    .star-item:before {
      -webkit-transition: color 200ms;
              transition: color 200ms;
      -webkit-transform: translate3d(0, 0, 0);
              transform: translate3d(0, 0, 0);
      will-change: color;
      content: '05';
    }
    
    /* Ao passar o mouse no container das estrelas, todos os itens ficam como marcados */
    /* Esconde os input radio de forma que continuem acessíveis via teclado */
    /* Ordena os itens da direita para a esquerda */
    .star:dir(rtl) .star-item, .star.rtl .star-item {
      direction: rtl;
    }
<div class='star'>
  <input name='rating' id='star-1' value='1' type='radio' class='radio radio-1 required'/>
  <input name='rating' id='star-2' value='2' type='radio' class='radio radio-2'/>
  <input name='rating' id='star-3' value='3' type='radio' class='radio radio-3'/>
  <input name='rating' id='star-4' value='4' type='radio' class='radio radio-4'/>
  <input name='rating' id='star-5' value='5' type='radio' class='radio radio-5'/>
  <label for='star-1' class='star-item star-item-1'>1 stars</label>
  <label for='star-2' class='star-item star-item-2'>2 stars</label>
  <label for='star-3' class='star-item star-item-3'>3 stars</label>
  <label for='star-4' class='star-item star-item-4'>4 stars</label>
  <label for='star-5' class='star-item star-item-5'>5 stars</label>
  <span>Vote</span>
</div>
    
22.03.2017 / 19:04