Improving Hover scheme for website reviews

Asked

Viewed 137 times

0

I’m trying to develop an evaluation scheme of files that are hosted on my client’s website. Each file gains beyond the access link a DIV with the five evaluation options, which appear as stars in gray. 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" <?php if($s==1){ echo "style='margin-left: 5px'"; } ?> />
    <?php } ?>
</div>

And the Javascript code, when you pass a star it changes color. And when you click, it warns 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:

  • Pass through star 5 and they all change color.
  • Pass through star 4 and only stars 1, 2, 3 and 4 change color.
  • Pass through star 3 and only stars 1, 2 and 3 change color.
  • Pass through star 2 and only stars 1 and 2 change color.
  • Passes through star 1 and only it changes color.

Because at the moment, only the respective star changes color when you pass through it. Does anyone know?

2 answers

1

Hello, well I’ve made a very similar script I think it 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: '\2605';
    }
    
    /* 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>

  • Your code is excellent, but here’s the thing: I have a list of files on the page and, next to each file link, there will be stars to evaluate. In your method within my file code, he always keeps the number chosen on the first line. And the idea is that when you click on one of the five stars, you automatically vote. If you know any more ideas...

  • Use ajax by passing the vote and the item you are voting on

1

Huum.. 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.