简体   繁体   中英

How do I make a progress bar?

I'm trying to create a progress bar for a product card track so by any click of the user on the prev and next buttons (which would scroll back or forward) the progress bar would advance or backup. here's the code I came up with. the problem is the first click doesn't show any result and the prev button acts like the next button for the first time. It's like the code is one step behind. I'm very new to javaScript and I can't figure out how this could happen.

const productScroll = () => {

  rightButton.onclick = function () {
   
    let scrollLeft = document.querySelector('#ProductSlider').scrollLeft;
    let scrollPercent = ((scrollLeft - 0) / (5033 - 0)) * (100 - 0) + 0;
    document.querySelector('div.progress-bar').style.width = `${scrollPercent}%`;
    
  };

  leftButton.onclick = function () {

    let scrollLeft = document.querySelector('#ProductSlider').scrollLeft;
    let scrollPercent = ((scrollLeft - 0) / (5033 - 0)) * (100 - 0) + 0;
    document.querySelector('div.progress-bar').style.width = `${scrollPercent}%`;
  };

If youre always 1 step behind, it could be that your percentage calculation is wrong. For example, if you have 5 steps and want to show progress for each step, starting at 1 and ending at 5, your progress bar needs to have 4 steps instead:

1 > 2 > 3 > 4 > 5 = 4 steps (total - 1)

In percentages, it looks like this for a 5 step progress bar:

1: 0%
2: 25%
3: 50%
4: 75%
5: 100%

Notice each increase is 25% (1/4) and not 20% (1/5).

So in abstract shape, your calculation would need to be:

((scroll / max) * (steps - 1)) / (steps - 1) * 100%

Which means your scrollLeft / 5033 needs to be between 0 and 4, divided by 4, then turned into a percentage:

const percentage = ((scrollLeft / 5033) * 4) / 4 * 100;

To create a progress bar, first create two “div” tag elements named id Progress_Status and myprogressbar .

To add a numeric label to indicate how far the user is in the process, the element inside or outside the progress bar is required which will display the progress status which in this case is myprogressbar .

<div id="Progress_Status"> 
  <div id="myprogressBar">1%</div> 
</div> 

Adding CSS:

#Progress_Status { 
  width: 50%; 
  background-color: #ddd; 
} 
  
#myprogressBar { 
  width: 1%; 
  height: 35px; 
  background-color: #4CAF50; 
  text-align: center; 
  line-height: 32px; 
  color: black; 
} 

Adding JavaScript:

Next, Code below creates a dynamic progress bar(animated) using JavaScript functions update and scene .

function update() { 
  var element = document.getElementById("myprogressBar");    
  var width = 1; 
  var identity = setInterval(scene, 10); 
  function scene() { 
    if (width >= 100) { 
      clearInterval(identity); 
    } else { 
      width++;  
      element.style.width = width + '%';  
      element.innerHTML = width * 1  + '%'; 
    } 
  } 
} 
  

Firstly, you fetch the element by id and then set starting width to 1. For the purpose of working example, i used setintervel to show the progression of progress bar.

All we are doing here is calling the scene function which checks if width is less than 100. If yes, then stop loader by clearing the interval. If not, then increment the width and add it to the with and progress label div for showing the progress in percentenge.

Complete Code:

 function update() { var element = document.getElementById("myprogressBar"); var width = 1; var identity = setInterval(scene, 10); function scene() { if (width >= 100) { clearInterval(identity); } else { width++; element.style.width = width + '%'; element.innerHTML = width * 1 + '%'; } } }
 #Progress_Status { width: 50%; background-color: #ddd; } #myprogressBar { width: 1%; height: 35px; background-color: #4CAF50; text-align: center; line-height: 32px; color: black; }
 <:DOCTYPE html> <html> <body> <h3>Example of Progress Bar Using JavaScript</h3> <p>Download Status of a File:</p> <div id="Progress_Status"> <div id="myprogressBar">1%</div> </div> <br> <button onclick="update()">Start Download</button> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM