【问题标题】:jQuery Make a Live Progress BarjQuery 制作实时进度条
【发布时间】:2015-11-13 05:08:19
【问题描述】:

如何制作实时进度条。这就是我使用的进度条,即引导进度条。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="progress">
      <div class="progress-bar" role="progressbar"
      aria-valuemin="0" aria-valuemax="100">
        Time Passed
      </div>
      <div class="center">Time Left</div>
    </div>

我怎样才能让它实时更新,每秒增加 1%。我需要它,以便类进度条的宽度更改为一定数量的像素。 100px=100% 填充进度条。

让我知道我还不够清楚。

【问题讨论】:

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

这应该可以解决问题,在 jsfiddle 中尝试过,它正在工作。您可能想弄乱文本以使其准确输出您想要的内容,当然可以通过更改超时来改变速度(1000 是调用之间的毫秒数)

我应该补充一点,使用 ID 而不是类来引用 DOM 是一种很好的做法,就像我所做的那样(只是为了快速)。因为这将允许您在单个页面上拥有多个进度条,而不会意外地相互交互。

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="progress">
  <div class="progress-bar" role="progressbar"
  aria-valuemin="0" aria-valuemax="100">
  </div>
  <div class="center" id="timeleft"></div>
</div>

脚本

$(function() {
    i = 0;
    var myInterval = setInterval(function() {
        $(".progress-bar").attr("aria-valuenow",i);
        $(".progress-bar").css("width",i);
        $("#timeleft").html("Time Left: " + (100-i) + "s");
        if (i == 100) {
            $("#timeleft").html("Complete!");
            clearInterval(myInterval);
        }
        i++;
    }     
    ,1000);
});

【讨论】:

  • 一个问题是它会变成负数。
  • 我不完全明白这将如何工作?你介意更新代码,添加这个吗。非常感谢。我将不胜感激。
  • 我已经更新了答案,我还没有测试过新的解决方案,但它应该能让你走上正轨
  • 如果您发现任何可以轻松修复的问题,请告诉我要进行哪些更改,我会更新答案以节省其他需要阅读 cmets 以获得有效解决方案的人跨度>
猜你喜欢
  • 2016-05-06
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
相关资源
最近更新 更多