Skip to content Skip to sidebar Skip to footer

How To Change The Color Of The Timeline Circle When It Is An Active Stage?

I have created a vertical timeline. Now I have to set the background color of the circle when it is an active stage. You can check the below image first circle with text is an acti

Solution 1:

I simplified your code ( deleted the code that is not for the question ) .

HTML

I added a relation between the buttons and the spans. With data-target id of span.

JQ

EDIT after op comment

First, you get the targeted span id ( when click on button2, span with id #button2 will be selected )

Second, get the span before the current selected one ( if there is one ) . Example when click on button2 , prevSelected will have value #button1

Then add and remove classes to the span and the links

See edited code below

CSS

add styles for the greenSpan class

Observation

  1. No need to add multiple $(document).ready(function(){. Nest all your code inside only 1 function
  2. Instead of giving active class to span and active class to a, you could give that active class to the li containing span and a and then just style in css, for example li.active > span {/*timeline-circle-active css*/} and li.active > a {/*timeline-text-active css*/}

See snippet below.

$(document).ready(function() {

  $('.button-clicked').click(function() {

    varTargetSpan = "#" + $(this).attr("data-target"),
        prevSelected = $(TargetSpan).parents("li").prev("li").find("span")
      
    prevSelected.addClass("greenSpan").removeClass("timeline-circle-active")
    prevSelected.next("a").addClass("greenLink").removeClass("timeline-text-active")
    $(TargetSpan).addClass("timeline-circle-active").removeClass("greenSpan")
    $(TargetSpan).next("a").addClass("timeline-text-active")


  });
});
.info-timelineul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.info-timelineulli {
  margin: 010px;
}

.info-timelineullispan {
  position: relative;
  border: 2px solid #000;
  border-radius: 100%;
  width: 45px;
  line-height: 40px;
  text-align: center;
  margin-top: 30px;
  color: #000;
  z-index: 2;
  display: inline-block;
}

.info-timelineullispan.timeline-circle-active {
  background-color: #ff0000;
  color: #000;
  border: 1px solid #ffff00!important;
}

.info-timelineullia.timeline-text-active {
  color: #ff0000!important;
}

.info-timelineullia.greenLink {
  color: green
}

.info-timelineulli:not(:first-of-type) span:before {
  position: absolute;
  border: 1px solid #000;
  width: 0;
  height: 30px;
  display: block;
  content: '';
  left: 50%;
  z-index: 1;
  top: -32px;
  margin-left: -1px;
}

.info-timelineulli:first-child {
  margin-top: 0;
}

.info-timelineulli:first-child:before {
  display: none;
}

.info-timelineullia {
  color: #000;
  margin: 10px;
}

.info-timelineullispan.greenSpan {
  background: green
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="info-timeline"><ul><li><spanid="button1"class=" timeline-circle-active">1</span><ahref="#"class="timeline-text-active">Button1</a></li><li><spanid="button2">2</span><ahref="#">Button2</a></li><li><spanid="button3">3</span><ahref="#">Button3</a></li><li><spanid="button4">4</span><ahref="#">Button4</a></li></ul></div><!--info-timeline--><buttontype="submit"class="button-clicked"data-target="button1">Button1</button><buttontype="submit"class="button-clicked"data-target="button2">Button2</button><buttontype="submit"class="button-clicked"data-target="button3">Button3</button><buttontype="submit"class="button-clicked"data-target="button4">Button4</button>

Post a Comment for "How To Change The Color Of The Timeline Circle When It Is An Active Stage?"