Home >Web Front-end >JS Tutorial >How to implement a simple progress bar effect in js (code example)
What this article brings to you is to introduce the method of realizing a simple progress bar effect in js (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Not much to say, just go to the code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ProgressBar</title> <style> *{ margin: 0; padding: 0; } #progress{ width: 100%; height: 30px; background: rgb(42, 138, 248); } #bar{ width: 1%; height: 28px; margin-top: 1px; background: purple; } </style> </head> <body> <div id="progress"> <div id="bar"></div> </div> <div><h3 id="text-progress">0%</h3></div> <input type="button" id=“btn” value="点击开始" onclick="action()"> </body> <script> function action(){ var iSpeed=1; obj=setInterval(function(){ iSpeed+=1; if(iSpeed>=100){ // 设置达到多少进度后停止 clearInterval(obj); } bar.style.width=iSpeed+'%'; document.getElementById('text-progress').innerHTML=iSpeed+'%'; },100); // 1s后函数执行一次 } </script> </html>
Result
##After I finished writing, I found a bug, click Start and then click Progress again The bar will be executed againSolution:1. After clicking start, set the button's disabled to disabled so that it cannot be clicked again2. Add judgment and give message prompt, if the progress bar is clicked again during progress, an alter prompt will be givenThe above is the detailed content of How to implement a simple progress bar effect in js (code example). For more information, please follow other related articles on the PHP Chinese website!