简体   繁体   English

我的 HTML、CSS 和 JS 进度条不起作用

[英]My HTML, CSS and JS progress bar doesn't work

I'm trying to do a progress bar so that the users can know when will their file be downloaded.我正在尝试制作一个进度条,以便用户可以知道何时下载他们的文件。 I don't know what I did wrong, but the progress bar style doesn't update in the for loop.我不知道我做错了什么,但进度条样式不会在 for 循环中更新。

 var filesize = 1000 //this is not the final value var progressbar = document.getElementById("progress"); function myFunction(){ for(var i = 0; i <= filesize; i++){ x = i/ filesize * 100; x = parseInt(x.toString().slice(0, 3)); console.log(x + "%") progressbar.style.width = x + "%"; } }
 #bar{ width: 35%; background-color: rgba(0,0,0,0.17); border-radius: 130px; margin: auto; } #progress{ width: 0%; height: 30px; background-color: rgb(255, 0, 0); border-radius: 130px; }
 <button onclick="myFunction()">Click me</button> <div id="bar"> <div id="progress"></div> </div>

You can make the progress bar move smoothly by adding the transition property in CSS.您可以通过在 CSS 中添加transition属性来使进度条平滑移动。

A higher transition time will result in the progress bar moving slower.过渡时间越长,进度条移动越慢。

 var filesize = 1000 //this is not the final value var progressbar = document.getElementById("progress"); function myFunction() { for (var i = 0; i <= filesize; i++) { x = i/ filesize * 100; x = parseInt(x.toString().slice(0, 3)); console.log(x + "%") progressbar.style.width = x + "%"; } }
 #bar { width: 35%; background-color: rgba(0,0,0,0.17); border-radius: 130px; margin: auto; } #progress { width: 0%; height: 30px; background-color: rgb(255, 0, 0); border-radius: 130px; transition: 0.2s; }
 <button onclick="myFunction()">Click me</button> <div id="bar"> <div id="progress"> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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