asohasf1241
asohasf1241

Reputation: 195

step progress bar

I have a progress bar with 4 different steps. Is there a way to make it so when you press the next button, it changes the four test buttons to something else?

Basically, I want to have 4 different pages under the 4 steps. So when you are on step 1, it shows you a page with buttons. When you click next, step 2 shows you something different like a line of text. Etc...

If you are struggling to understand, here is a codepen with the result I desire: https://codepen.io/vajahath_ahmed/pen/xEgOdp notice how when you press next, the text changes. That is what I'm trying to achieve.

$(document).ready(function() {
  $('.step').each(function(index, element) {
    // element == this
    $(element).not('.active').addClass('done');
    $('.done').html('<i class="icon-ok"></i>');
    if ($(this).is('.active')) {
      return false;
    }
  });
  $('.step>i.icon-ok').hide();
});

const iconClasses = ['far fa-hand-pointer', 'fas fa-mortar-pestle', 'fas fa-shipping-fast', 'fas fa-shopping-cart'];

function next() {
  //console.log("Next", Math.random());
  let latestActiveStep = $("div.step.active").not(".done");
  let stepNumber = +$(latestActiveStep).data("stepnum");
  console.log("step", stepNumber);
  $(latestActiveStep).addClass("done");
  $(latestActiveStep).find("i.icon-ok").toggle();
  $(latestActiveStep).find("i").not(".icon-ok").toggle();
  $(latestActiveStep).next().addClass("active");
}

function previous() {
  //console.log("Prev", Math.random());
  let latestDoneStep = $("div.step.active.done").last();
  let stepNumber = +$(latestDoneStep).data("stepnum");
  console.log("step", stepNumber);
  $(latestDoneStep).removeClass("done");
  $(latestDoneStep).next().removeClass("active");
  $(latestDoneStep).find("i.icon-ok").toggle();
  $(latestDoneStep).find("i").not(".icon-ok").toggle();
}
@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,700,600);
body {
  background-color: #e6e6e6;
  font-family: 'Open Sans', sans-serif;
}

#steps {
  /* width: 505px; */
  /* margin: 50px auto; */
  width: auto;
  margin: 10px 0 10px 50px;
}

.step {
  width: 40px;
  height: 40px;
  background-color: white;
  display: inline-block;
  border: 4px solid;
  border-color: transparent;
  border-radius: 50%;
  color: #cdd0da;
  font-weight: 600;
  text-align: center;
  line-height: 35px;
}

.step:first-child {
  line-height: 40px;
}

.step:nth-child(n+2) {
  margin: 0 0 0 100px;
  transform: translate(0, -4px);
}

.step:nth-child(n+2):before {
  width: 75px;
  height: 3px;
  display: block;
  background-color: white;
  transform: translate(-95px, 21px);
  content: '';
}

.step:after {
  width: 150px;
  display: block;
  transform: translate(-55px, 3px);
  color: #818698;
  content: attr(data-desc);
  font-weight: 400;
  font-size: 13px;
}

.step:first-child:after {
  transform: translate(-55px, -1px);
}

.step.active {
  border-color: #dcc2f2;
  color: #dcc2f2;
}

.step.active:before {
  background: linear-gradient(to right, #ffb5d5 0%, #dcc2f2 100%);
}

.step.active:after {
  color: #ffb5d5;
  font-weight: bold;
}

.step.done {
  background-color: #ffb5d5;
  border-color: #ffb5d5;
  color: white;
}

.step.done:before {
  background-color: #ffb5d5;
}

.step i {
  position: relative;
  bottom: 3px;
}
.as-console-wrapper {
  max-height: 50px!important;
  top: auto;
  left: auto;
  right: auto;
  bottom: 0;
}
<html style="width: 100%;height: 100%;">

<head>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body style="width: 100%; background-color: orange;">

  </section>
  <section id="newsut">
    <link href="https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
    <script src="https://kit.fontawesome.com/a076d05399.js"></script>

    <div id="steps">
      <div class="step active" data-stepnum="0" data-desc="Listing information"><i class="fas fa-hand-pointer"></i><i class="icon-ok"></i></div>
      <div class="step" data-stepnum="1" data-desc="Photos & Details"><i class="fas fa-mortar-pestle"></i><i class="icon-ok"></i></div>
      <div class="step" data-stepnum="2" data-desc="Review & Post"><i class="fas fa-shipping-fast"></i><i class="icon-ok"></i></div>
      <div class="step" data-stepnum="3" data-desc="Your order"><i class="fas fa-shopping-cart"></i><i class="icon-ok"></i></div>
    </div>

    <a>Test</a>
    <a>Test2</a>
    <a>Test3</a>
    <a>Test4</a>
  </section>

  <section id="nextpad">
    <button class="previous" onclick="previous()"><span>Previous</span></button>
    <button class="next" onclick="next()"><span>Next</span></button>
  </section>

</body>

</html>

Upvotes: 3

Views: 1865

Answers (1)

ElBrm
ElBrm

Reputation: 344

Since you are using the data-stepnum attribute as a counter, you can use this to trigger different divs. Check this out:

1. Create content divs.

We create a content div for each tab, with the class `step-content` and the attribute `data-stepnum`. Make sure the value of the data-stepnum is the same as each tabs. This attribute is the part where we connect the tab with the content. Notice the `active` class add the first div, because the first content tab is active as well.
<div id="steps-content">
  <div class="step-content active" data-stepnum="0">
    <h2>Step content 1</h2>
  </div>
  <div class="step-content" data-stepnum="1">
    <h2>Step content 2</h2>
  </div>
  <div class="step-content" data-stepnum="2">
    <h2>Step content 3</h2>
  </div>
  <div class="step-content" data-stepnum="3">
    <h2>Step content 4</h2>
  </div>
</div>

2. Hide the content divs with css

Now we're gonna hide the content divs by default with css. Only the active class should be visible.
.step-content{
  display: none;
}

.step-content.active{
  display: block;
}

3.Add the trigger with jQuery

You already got the `data-stepnum` value of the active class in a variable `stepNumber`, so we can use this to add the active class to the right content.

We're gonna change the next() and previous() functions. First create a variable with the content classname, just to make it easier.

var stepContentClass = '.step-content';

Next, update the next() function. When the function runs, we want to hide all content so we have to remove all active classes.

$(stepContentClass).removeClass('active');

Now we want to show the right content. You already saved the active stepNumber, so we select the right content with this data-stepnum attribute.

$(stepContentClass + '[data-stepnum= ' + stepNumber + ']').next().addClass('active');

You can do the same with the previous() function.

Full functions:

function next() {
  //console.log("Next", Math.random());
  let latestActiveStep = $("div.step.active").not(".done");
  let stepNumber = +$(latestActiveStep).data("stepnum");
  var stepContentClass = '.step-content';
  console.log("step", stepNumber);
  $(latestActiveStep).addClass("done");
  $(latestActiveStep).find("i.icon-ok").toggle();
  $(latestActiveStep).find("i").not(".icon-ok").toggle();
  $(latestActiveStep).next().addClass("active");
  $(stepContentClass).removeClass('active');
  $(stepContentClass + '[data-stepnum= ' + stepNumber + ']').next().addClass('active');
}

function previous() {
  //console.log("Prev", Math.random());
  let latestDoneStep = $("div.step.active.done").last();
  let stepNumber = +$(latestDoneStep).data("stepnum");
  var stepContentClass = '.step-content';
  console.log("step", stepNumber);
  $(latestDoneStep).removeClass("done");
  $(latestDoneStep).next().removeClass("active");
  $(latestDoneStep).find("i.icon-ok").toggle();
  $(latestDoneStep).find("i").not(".icon-ok").toggle();
  $(stepContentClass).removeClass('active');
  $(stepContentClass + '[data-stepnum= ' + stepNumber + ']').addClass('active');
}

Result

Check this JSFiddle for full code: https://jsfiddle.net/0p9vekm7/

Upvotes: 1

Related Questions