jQuery 显示时间倒计时进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24530908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing a time countdown progress bar
提问by Elton
I came across this progress bar code on this site.
我在这个网站上遇到了这个进度条码。
What I want to do is, have a 3 minute countdown and show how many minutes and seconds are left. So rather than displaying percentage I want to display time left.
我想要做的是,倒计时 3 分钟并显示还剩多少分钟和秒。因此,我想显示剩余时间而不是显示百分比。
I use the following code:
我使用以下代码:
JS
JS
progress(100, $('#progressBar')); // This is what the timer should update each second
function progress(percent, $element) {
var progressBarWidth = percent * $element.width() / 100;
$element.find('div').animate({ width: progressBarWidth }, 500).html(percent + " minutes/seconds to go");
};
HTML
HTML
<div id="progressBar">
<div></div>
</div>
CSS
CSS
#progressBar {
width: 923px;
margin: 10px auto;
height: 22px;
background-color: #0A5F44;
}
#progressBar div {
height: 100%;
text-align: right;
padding: 0 10px;
line-height: 22px; /* same as #progressBar height if we want text middle aligned */
width: 0;
background-color: #CBEA00;
}
回答by zessx
You'll need to change a bit this function :
您需要稍微更改此功能:
- Use 3 parameters (
timeleft
,timetotal
,element
) instead of the 2 actual - Use
setTimeout()
to refresh your progress bar every second - Check
timeleft
to know if you needto refresh the progress bar
- 使用 3 个参数 (
timeleft
,timetotal
,element
) 而不是 2 个实际参数 - 用于
setTimeout()
每秒刷新进度条 - 查看
timeleft
是否需要刷新进度条
function progress(timeleft, timetotal, $element) {
var progressBarWidth = timeleft * $element.width() / timetotal;
$element
.find('div')
.animate({ width: progressBarWidth }, 500)
.html(timeleft + " seconds to go");
if(timeleft > 0) {
setTimeout(function() {
progress(timeleft - 1, timetotal, $element);
}, 1000);
}
};
progress(180, 180, $('#progressBar'));
Plus, you should add this CSS, to avoid your progress bar to overflow its container :
另外,您应该添加此 CSS,以避免进度条溢出其容器:
#progressBar div {
box-sizing: border-box;
}
Snippet:
片段:
function progress(timeleft, timetotal, $element) {
var progressBarWidth = timeleft * $element.width() / timetotal;
$element.find('div').animate({ width: progressBarWidth }, 500).html(timeleft + " seconds to go");
if(timeleft > 0) {
setTimeout(function() {
progress(timeleft - 1, timetotal, $element);
}, 1000);
}
};
progress(180, 180, $('#progressBar'));
#progressBar {
width: 90%;
margin: 10px auto;
height: 22px;
background-color: #0A5F44;
}
#progressBar div {
height: 100%;
text-align: right;
padding: 0 10px;
line-height: 22px; /* same as #progressBar height if we want text middle aligned */
width: 0;
background-color: #CBEA00;
box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="progressBar">
<div></div>
</div>
[Tip]
[提示]
If you want your progress bar to smoothly and constantly decrease, use this code instead :
如果您希望进度条平稳并不断减少,请改用以下代码:
.animate({ width: progressBarWidth }, timeleft == timetotal ? 0 : 1000, "linear")