【问题标题】:Twitter bootstrap progress bar animation on page load页面加载时的 Twitter 引导进度条动画
【发布时间】:2023-03-08 05:03:01
【问题描述】:

我有一个带有几个引导进度条的页面。设置它们的值最初可以正常工作。虽然我希望进度条在用户打开页面时动画/转换到其特定状态。

当您单击其中一个栏时,此 JS 工作正常。在酒吧的“onload”事件中,我需要类似的东西。但“onload”事件对 s 不可用

//animate progress bars
$('.progress .bar').on("click", function(event) {
  var me = $(this);
  perc = me.attr("data-percentage");
  me.css('width', perc+'%');
});

如何在页面加载时为页面上的所有进度条实现此行为?

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    编辑

    • 类名在 v3.1.1 中从 bar 更改为 progress-bar

    HTML

    <div class="container">
        <div class="progress progress-striped active">
            <div class="bar" style="width: 0%;"></div>
        </div>
    </div>​
    

    CSS

    @import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css');
    
    .container {
        margin-top: 30px;
        width: 400px;
    }​
    

    jQuery 用于下面的小提琴和document.ready

    $(document).ready(function(){
    
        var progress = setInterval(function() {
            var $bar = $('.bar');
    
            if ($bar.width()>=400) {
                clearInterval(progress);
                $('.progress').removeClass('active');
            } else {
                $bar.width($bar.width()+40);
            }
            $bar.text($bar.width()/4 + "%");
        }, 800);
    
    });​
    

    演示

    JSFiddle

    Updated JSFiddle

    【讨论】:

    • 虽然此解决方案适用于单个文件下载之类的问题,但它不适用于像我这样具有多个进度条元素的页面。
    • @Alao 这可能有助于理解:stackoverflow.com/questions/8347248/…:)
    【解决方案2】:

    虽然 Tats_innit 的回答给人一种很好的感觉,但我不得不做一些不同的事情,因为我在页面上有多个进度条。

    这是我的解决方案:

    JSfiddle:http://jsfiddle.net/vacNJ/

    HTML(示例):

    <div class="progress progress-success">
    <div class="bar" style="float: left; width: 0%; " data-percentage="60"></div>
    </div>
    
    <div class="progress progress-success">
    <div class="bar" style="float: left; width: 0%; " data-percentage="50"></div>
    </div>
    
    <div class="progress progress-success">
    <div class="bar" style="float: left; width: 0%; " data-percentage="40"></div>
    </div>
    

    JavaScript:

    setTimeout(function(){
    
        $('.progress .bar').each(function() {
            var me = $(this);
            var perc = me.attr("data-percentage");
    
            var current_perc = 0;
    
            var progress = setInterval(function() {
                if (current_perc>=perc) {
                    clearInterval(progress);
                } else {
                    current_perc +=1;
                    me.css('width', (current_perc)+'%');
                }
    
                me.text((current_perc)+'%');
    
            }, 50);
    
        });
    
    },300);
    

    @Tats_innit:使用 setInterval() 动态重新计算进度是一个不错的解决方案,谢谢伙计! ;)

    编辑:

    我的一个朋友为自定义 twitter 引导进度条编写了一个不错的 jquery 插件。 这是一个演示: http://minddust.github.com/bootstrap-progressbar/

    这是 Github 存储库: https://github.com/minddust/bootstrap-progressbar

    【讨论】:

    • 它工作得很好.. 但有时它会在进度条中显示十进制值,并且 % 从进度条中消失了......
    • 嗨 - 有人能告诉我如何编辑这个脚本来让它十分钟吗?我玩过,但我似乎无法获得十分钟规则 - 有什么想法吗?非常感谢
    【解决方案3】:

    Bootstrap 使用 CSS3 过渡,因此当您通过 javascript / jQuery 设置 .bar 的宽度时,进度条会自动设置动画。

    http://jsfiddle.net/3j5Je/ ..看到了吗?

    【讨论】:

      【解决方案4】:

      对 ellabeauty 的回答作出贡献。你也可以使用这个动态百分比值

      $('.bar').css('width',  function(){ return ($(this).attr('data-percentage')+'%')});
      

      并且可能会在你的 CSS 中添加自定义缓动

      .bar {
       -webkit-transition: width 2.50s ease !important;
       -moz-transition: width 2.50s ease !important;
         -o-transition: width 2.50s ease !important;
            transition: width 2.50s ease !important;
       }
      

      【讨论】:

        【解决方案5】:

        这是一个跨浏览器的纯 CSS 解决方案。希望对您有所帮助!

        DEMO

        .progress .progress-bar {
            -moz-animation-name: animateBar;
            -moz-animation-iteration-count: 1;
            -moz-animation-timing-function: ease-in;
            -moz-animation-duration: .4s;
        
            -webkit-animation-name: animateBar;
            -webkit-animation-iteration-count: 1;
            -webkit-animation-timing-function: ease-in;
            -webkit-animation-duration: .4s;
        
            animation-name: animateBar;
            animation-iteration-count: 1;
            animation-timing-function: ease-in;
            animation-duration: .4s;
        }
        
        @-moz-keyframes animateBar {
            0% {-moz-transform: translateX(-100%);}
            100% {-moz-transform: translateX(0);}
        }
        @-webkit-keyframes animateBar {
            0% {-webkit-transform: translateX(-100%);}
            100% {-webkit-transform: translateX(0);}
        }
        @keyframes animateBar {
            0% {transform: translateX(-100%);}
            100% {transform: translateX(0);}
        }
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
        
        <div class="container">
          
          <h3>Progress bar animation on load</h3>
          
          <div class="progress">
            <div class="progress-bar progress-bar-success" style="width: 75%;"></div>
          </div>
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-11
          • 1970-01-01
          • 2012-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多