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

211
Vistas
Add fade-in/fade-out to background change

i'm pretty new to javascript but I finally could get this working. I can change the backgroundColor. But it's switching hard and I was wondering, if I can add a fade-in/fade-out if I change the backgroundColor from one to the other button.

var btn1 = document.getElementById("tab1");
var btn2 = document.getElementById("tab2");

btn1.addEventListener('click', function (event) {
    changeColor('blue');
});
btn2.addEventListener('click', function (event) {
    changeColor('green');
});

function changeColor(color){
  var elem = document.getElementById( "area" );
  elem.style.backgroundColor =color;

I was also thinking about to change the colors to images.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can set a transition to the element by CSS like this:

button {
    -webkit-transition: background-color 1000ms linear;
    -ms-transition: background-color 1000ms linear;
    transition: background-color 1000ms linear;
}

I recommend checking out this documentation

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use transition to animate a style change, then use classList.add and classList.remove to add/remove the classes that contain the new color.

var btn1 = document.getElementById("tab1");
var btn2 = document.getElementById("tab2");

btn1.addEventListener('click', function(event) {
  changeColor('blue');
});
btn2.addEventListener('click', function(event) {
  changeColor('green');
});

function changeColor(color) {
  var elem = document.getElementById("area");
  elem.classList.remove('blue', 'green')
  elem.classList.add(color);
}
#area {
  padding: 50px;
  margin-bottom: 15px;
  transition: background-color 1s;
}

.green {
  background-color: green;
}

.blue {
  background-color: blue;
}
<div id='area'></div>
<button id='tab1'>button 1 </button>
<button id='tab2'>button 2 </button>

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