达到 100% 时关闭 Bootstrap 进度条
Close Bootstrap progress bar upon reaching 100% full
我正在做一个 while
循环来填充我的 Bootstrap 进度条,从 0
到 100
。
这可行,但是我想在进度条达到 100% 时隐藏它。我尝试使用 setTimeOut
但没有成功。
尽管 0-100 的 while
循环可以在一微秒内完成,但条形图仍会逐渐填满。
var i = 0;
while(i <= 100){
$('#progress').attr('aria-valuenow', i).css('width', i + '%');
i++;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="progress">
<div id="progress" class="progress-bar progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
全部完成。没有 Bootstrap 更容易。
updateProgress(0);
function updateProgress(i) {
setTimeout(function() {
if (i <= 100) {
$('#progress').width(i + '%');
i++;
updateProgress(i);
} else {
$('#progress').hide();
$('#progress').width('1%');
}
}, 4);
}
#progress {
background-color: #007bff;
height: 3px;
left: 0;
position: absolute;
right: 0;
top: 0;
width: 1%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="progress"></div>
我正在做一个 while
循环来填充我的 Bootstrap 进度条,从 0
到 100
。
这可行,但是我想在进度条达到 100% 时隐藏它。我尝试使用 setTimeOut
但没有成功。
尽管 0-100 的 while
循环可以在一微秒内完成,但条形图仍会逐渐填满。
var i = 0;
while(i <= 100){
$('#progress').attr('aria-valuenow', i).css('width', i + '%');
i++;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="progress">
<div id="progress" class="progress-bar progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
全部完成。没有 Bootstrap 更容易。
updateProgress(0);
function updateProgress(i) {
setTimeout(function() {
if (i <= 100) {
$('#progress').width(i + '%');
i++;
updateProgress(i);
} else {
$('#progress').hide();
$('#progress').width('1%');
}
}, 4);
}
#progress {
background-color: #007bff;
height: 3px;
left: 0;
position: absolute;
right: 0;
top: 0;
width: 1%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="progress"></div>