简体   繁体   English

如何使用 Twitter Bootstrap 和 Jquery 制作 Hipmunk 风格的进度条

[英]How To Make a Hipmunk Style Progress Bar with Twitter Bootstrap and Jquery

To my surprise, I'm not seeing much for this.令我惊讶的是,我对此并没有看到太多。

The bootstrap docs give plenty of options for displaying a progress bar, but no instructions for actually making it do something. bootstrap 文档提供了大量显示进度条的选项,但没有实际操作的说明。

I'm writing a one page web app that'll ideally use a progress bar transition before switching to the hidden part of the page.我正在编写一个单页 web 应用程序,在切换到页面的隐藏部分之前,它最好使用进度条过渡。

Here's the simplified html:这是简化的html:

HTML HTML

<div id="part1">
    <p>Sample App</p>
        <button class="analyze btn btn-primary">Analyze</button>
</div>

<div id="part2">
    <!-- Some html goes here -->
</div>

CSS CSS

#part2 {
   display: none;
}

Jquery查询

$(".analyze").click(function() {
    $("#part1").hide();
    $("#part2").show();
});

This is very simple.这很简单。 What I'd like to do is make a progress bar that dynamically populates on $(".analyze").click and takes a fixed amount of time to complete before #part2 becomes visible using the bootstrap progress bar.我想做的是制作一个在 $(".analyze").click 上动态填充的进度条,并在 #part2 使用引导进度条变得可见之前需要固定的时间来完成。

Something very similar to what hipmunk, or many of the other airline aggregator sites do.与 hipmunk 或许多其他航空公司聚合网站所做的非常相似。

Ideally this is compatible across most browsers and uses jquery since this is what I'm using to make most of the UI for my app.理想情况下,这与大多数浏览器兼容并使用 jquery,因为这是我用来为我的应用程序制作大部分 UI 的内容。

You can make use of the JavaScript function setInterval to repeatedly run a function over and over again.您可以使用 JavaScript 函数setInterval一遍又一遍地重复运行一个函数。 So that is an easy way to change the width of something over a given amount of time.所以这是在给定的时间内改变事物宽度的简单方法。

HTML HTML

<div id="instance" class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
    <span class="sr-only">0% Complete</span>
  </div>
</div>

<div class="show-on-done hidden">here is some other stuff</div>

JavaScript: JavaScript:

function fakeProgress(container, durationInMs, onDone) {
    var intervalInMS = 200;
    var doneDelay = intervalInMS * 2;
    var bar = container.find('.progress-bar');
    var srOnly = bar.find('.sr-only');
    var percent = 0;

    var interval = setInterval(function updateBar() {
        percent += 100 * (intervalInMS/durationInMs);
        bar.css({width: percent + '%'});
        bar['aria-valuenow'] = percent;
        srOnly.text(percent + '% Complete');

        if (percent >= 100) {
            clearInterval(interval);
            setTimeout(function() {
                if (typeof onDone === 'function') {
                    onDone();
                }
            }, doneDelay);
        }
    }, intervalInMS);
}

Then call it with this JavaScript:然后用这个 JavaScript 调用它:

var duration = 1000;  // in milliseconds
function onDone() {
    $('.show-on-done').removeClass('hidden');
}

fakeProgress($('#instance'), duration, onDone);

JSFiddle: https://jsfiddle.net/6ht5umxz/3/ JSFiddle: https ://jsfiddle.net/6ht5umxz/3/

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

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