Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Hover keyframe values green and red

I'm kind of stuck in a keyframe. I have a table with values from a fetch (json) and i should add a hover when the values is less than 5 in red and starting from 5 in green. This is my code in javascript ->

how do i have to implement the keyframe with it in css or is it different that i think?

 // Html

 <div id="group3">
    <table class="table">
      <thead>
        <tr class="info">
          <th></th>
          <th>February</th>
          <th>March</th>
          <th>April</th>
          <th>May</th>
          <th>June</th>
          <th>July</th>
          <th>August</th>
        </tr>
      </thead>
      <tbody id='mytable'>
      </tbody>
    </table>
</div>

 //CSS
Keyframe?


 //hover

 let cells = document.querySelectorAll("tbody");
 cells.forEach( el => el.addEventListener('mouseenter', function () {
   if(el.textContent < 5){
     el.classList.add('underfive');
   } else if (el.textContent >=5){
     el.classList.add('abovefive');
   }
 }));

  // reset animationx
  cells.forEach(el => el.addEventListener('mouseleave', function () {
   if(el.textContent < 5){
     el.classList.remove('underfive');
   } else if (el.textContent >=5){
     el.classList.remove('abovefive');
   }
 }));

it should be like this ->

this is the startpage, background is white

this is the end result how it should be, uploaded from a json file in a table, red value

this is the green value when it's 5 of higher

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on what you say you want to show a different background color or styling in general than the default if the mouse is over the td.

So use :hover for that. You want to have a transition between those states so use transition.

You want to have a different color if it is above or below 5. So define what you want to have as default and add a class for the other case.

let data = [1,4,2,8,12,2,5,7];

const tr = document.querySelector('tr');
data.forEach(elem => {
   let td = document.createElement('td');
   td.textContent = elem;

   td.classList.toggle('belowfive', elem < 5);

   tr.appendChild(td);
});
td {
  transition: background-color 1s;
  background-color: white;
  padding: 1rem;
  border: 1px solid hsl(0 0% 50%);
}

td:hover {
   background-color: green;
}

td.belowfive:hover {
   background-color: red;
}
<table>
  <tbody>
     <tr>
     </tr>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda