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

94
Vistas
step process with JavaScript doesn't change colors

I am trying to make a step process with JavaScript. And I want it when it reaches the step 3, the color changes to another color. I tried a lot, but I didn't succeed.

Picture how step process works

Picture how step process works (with three green blocks)

Picture how I want it to work

Picture how I want it to work (with two green blocks and a red block)

$(document).ready(function() {
  var i = 1;

  var id = setInterval(steps, 700);

  function steps() {
    if (i == 4) {
      clearInterval(id);
    } else {
      $(".step" + i).css('background-color', 'rgb(134 209 109)');
      i++;
    }
  }
  $(".btn-next").on("click", function() {
    $('.page-1').fadeOut(1000, function() {
      $('.page-2').fadeIn(1000);

    });

    var interval = setInterval(slide, 1200)
    d = 1;

    function slide() {
      if (d == 6) {
        clearInterval(interval);
        $('.btn-next-two').fadeIn();
        $('.search').fadeOut();
      } else {
        $(".ship-" + d).addClass('slide-in-left');
        $(".ship-" + d).css('display', 'block');
        d++;
      }
    }
  });
  $(".btn-next-two").on("click", function() {
    $('.page-2').fadeOut(1000, function() {
      $('.page-3').fadeIn(1000);

    });
  });
  $('.btn-delivery').on('click', function() {
    $('.page-3').fadeOut(1000, function() {
      $('.page-4').fadeIn(1000);
    });
  });
  $('.btn-time').on('click', function() {
    $('.page-4').fadeOut(1000, function() {
      $('.page-5').fadeIn(1000);
    });
  });
});
step {
  min-height: 20px;
  margin-right: 2px;
  border-radius: 14px;
  background-color: #a9b7b9;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-2 col-sm-1  offset-2 offset-sm-4 step1 step">
</div>
<div class="col-2 col-sm-1  step2 step">
</div>
<div class="col-2 col-sm-1  step3 step">
</div>
<div class="col-2 col-sm-1  step4 step">

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

0

Just put

else if(i == 3){
           $(".step"+i).css('background-color','red');
           i++;
       }

on the condition.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There is a tiny issue in your javascript code.

I updated the function step.

function steps(){
  if(i == 5){
    clearInterval(id);
  }
  else if(i == 3) {
    $(".step"+i).css('background-color','red');
    i++;
  }
  else{
    $(".step"+i).css('background-color','rgb(134 209 109)');
    i++;
  }
}

Here is full code.

$(document).ready(function(){
      var i = 1;

      var id = setInterval(steps,700);
      function steps(){
           if(i == 5){
               clearInterval(id);
           }
           else if(i == 3) {
               $(".step"+i).css('background-color','red');
               i++;
           }
           else{
               $(".step"+i).css('background-color','rgb(134 209 109)');
               i++;
           }
      }
      $( ".btn-next" ).on( "click", function() {
      $('.page-1').fadeOut(1000, function(){
          $('.page-2').fadeIn(1000);

      });

      var interval = setInterval(slide,1200)
      d = 1;
      function slide(){
          if(d == 6){
              clearInterval(interval);
              $('.btn-next-two').fadeIn();
              $('.search').fadeOut();
          }
          else{
              $(".ship-"+d).addClass('slide-in-left');
              $(".ship-"+d).css('display','block');
              d++;
          }
      }
      });
      $( ".btn-next-two" ).on( "click", function() {
      $('.page-2').fadeOut(1000, function(){
          $('.page-3').fadeIn(1000);

      });
      });
      $('.btn-delivery').on('click',function(){
       $('.page-3').fadeOut(1000, function(){
          $('.page-4').fadeIn(1000);
          });});
      $('.btn-time').on('click',function(){
       $('.page-4').fadeOut(1000, function(){
          $('.page-5').fadeIn(1000);
          });});
      });
.step{
  min-height:20px;
  margin-right:2px;
  border-radius:14px;
  background-color:#a9b7b9;
}
<!DOCTYPE html>
<html lang="en">
  <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  </head>
  <body>
    <div class="col-2 col-sm-1  offset-2 offset-sm-4 step1 step">
    </div>
    <div class="col-2 col-sm-1  step2 step">
    </div>
    <div class="col-2 col-sm-1  step3 step">
    </div>
    <div class="col-2 col-sm-1  step4 step">

  </body>
</html>

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