Home >Web Front-end >JS Tutorial >How to create a dynamically loaded progress bar in js? (code example)

How to create a dynamically loaded progress bar in js? (code example)

青灯夜游
青灯夜游Original
2018-11-20 12:00:363771browse

The content of this article is to introduce how to create a dynamically loaded progress bar in js? (Code sample) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The progress bar can be used to describe the progress of any task being executed. It is usually used to display the status of downloads and uploads. In other words, a progress bar can be used to describe the status of anything in progress.

Let’s take a look at how to use JavaScript to implement a dynamically loaded progress bar? We need to perform the following steps:

1. Create an HTML structure for the progress bar:

The following code contains two elements named "Progress_Status" and "myprogressbar" "div" tag element.

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

2. Add CSS:

The following code is used to set the width and background color of the progress bar and the progress status in the bar chart.

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

3. Add JavaScript:

The following code uses the javascript functions "update" and "scene" to create a dynamic progress bar (animation).

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 + &#39;%&#39;;  
    } 
  } 
}

Run:

How to create a dynamically loaded progress bar in js? (code example)

## Such a loading style is a bit monotonous, we can also add numerical labels to indicate the user's position in the process; this needs to be done in the progress Add new elements inside or outside the bar to show progress status.

4. Add number loading style

We can add a statement in the else {} statement of JavaScript:


element.innerHTML = width * 1  + &#39;%&#39;;

That’s it The style of digital loading display progress status can be realized, and the running effect is:

How to create a dynamically loaded progress bar in js? (code example)

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related video tutorials, please visit:

JavaScriptTutorial!

The above is the detailed content of How to create a dynamically loaded progress bar in js? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn