Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda