Home > Web Front-end > JS Tutorial > body text

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

青灯夜游
Release: 2018-11-20 12:00:36
Original
3710 people have browsed it

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>
Copy after login

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; 
}
Copy after login

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;;  
    } 
  } 
}
Copy after login

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;;
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!