【问题标题】:How do you animate the value for a jQuery UI progressbar?您如何为 jQuery UI 进度条的值设置动画?
【发布时间】:2011-06-30 04:04:08
【问题描述】:

我已经设置了一个 jQuery UI 进度条,但不能使用 jQuery animate 来为它的值设置动画。关于如何完成这项工作的任何想法?

percentDone 变量包含一个从 0 到 100 的数字,表示滚动条应该有多远(这很好用)。

我尝试了几种不同的方法都无济于事。到目前为止,这是我所拥有的:

var progressbar = $("#progressbar1").widget();

progressbar.animate({
    value: percentDone
}, 5000, function() {
    console.debug('done animating');
});

请注意,如果我使用“值”方法更新滚动条,它可以正常工作,但它会跳转到该值而不是平滑地对其进行动画处理:

$("#progressbar1").progressbar('value', percentDone);

【问题讨论】:

    标签: jquery jquery-ui scrollbar jquery-animate


    【解决方案1】:
    $(function() {
        var pGress = setInterval(function() {
            var pVal = $('#progressbar').progressbar('option', 'value');
            var pCnt = !isNaN(pVal) ? (pVal + 1) : 1;
            if (pCnt > 100) {
                clearInterval(pGress);
            } else {
                $('#progressbar').progressbar({value: pCnt});
            }
        },10);
    });
    
    • DEMO 2::为了好,改编@Peter 的回复如下 ;-)。
    $(function() {
        $('#progressbar').progressbar(); // inizializa progressbar widget
        $pVal = $('.ui-progressbar-value').addClass('ui-corner-right');
        var pGress = setInterval(function() { //generate our endless loop
            var pCnt = $pVal.width(); // get width as int
            // generate a random number between our max 100 and it's half 50, 
            // this is optional, and make the bar move back and forth before
            // we reach the end.
            var rDom = Math.floor(Math.random() * (100 - 50 + 1) + 50);
            var step = rDom >= 100 ? 100: rDom; // reached our max ? reset step.
            doAnim(step);
        },1000);
        var doAnim = function(wD) {
            // complete easing list http://jqueryui.com/demos/effect/easing.html
            $pVal.stop(true).animate({width: wD + '%'},1000, 'easeOutBounce');
            if (wD >= 100) clearInterval(pGress) /* run callbacks here */
        }
    });
    

    在实际应用程序中,您可能不需要生成循环,例如,在上传文件时,您的 Flash 应用程序会告诉您文件大小,并在您达到所需的最大值时通知您,所以我的第一个代码是旨在仅演示进度条设置器和获取器的使用,当然还有什么使整个事情发挥作用,例如循环;第二个是对相同概念与糖的改编。

    【讨论】:

    • 我很高兴你喜欢它,兄弟! ;-)
    • 有没有办法在其中添加缓动属性?
    • 似乎你的动画函数覆盖了一些css并将'ui-progressbar-value'设置为显示:无;您需要再次覆盖它才能使其工作。至少我在 Chrome 中遇到了这个问题。
    • 这需要大量的工作来做一些应该很简单的事情。读者请注意以下其他解决方案。
    • @Vael Victus:在投反对票之前,请务必阅读完整答案 tnx!
    【解决方案2】:

    使用 CSS3 制作动画

    使用 CSS3,无需使用 JavaScript 直接管理进度条的值。只需将其添加到您的样式中即可:

    .ui-progressbar-value {
      transition: width 0.5s;
      -webkit-transition: width 0.5s;
    }
    

    您可以了解更多关于CSS3 Transitions的信息。

    【讨论】:

    • +1 用于回答已有两年多的问题并使用当前技术对其进行更新!谢谢!
    • 惊人的简单解决方案。
    【解决方案3】:

    这里是如何让它流畅地制作动画,而不是在@aSeptik 当前接受的答案中建议的有点生涩的方式。它绕过了.progressbar('value, ...) 方法。

    // On load, make the progressbar inside always have round edges:
    // (This makes it look right when 100%, and seems nicer all the time to me.)
    $("#progressbar .ui-progressbar-value").addClass("ui-corner-right");
    
    new_width = "50px";  // you will need to calculate the necessary width yourself.
    $("#progressbar .ui-progressbar-value").animate({width: new_width}, 'slow')
    

    【讨论】:

    • 起初我无法让它工作。我发现我需要将进度条值设置为大于 0 的值,即 $("#progressbar").progressbar({ value: 0.0001 }); 另外,另外,您可以使用 % 而不是 px new_width = "50%";
    【解决方案4】:

    jquery 论坛上一个很好的解决方案

    http://forum.jquery.com/topic/smooth-progressbar-animation

    进度条应该用 0.1 来初始化才能工作

    $("#progressbar .ui-progressbar-value").animate(
    {
      width: "67%"
    }, {queue: false});
    

    【讨论】:

    • 这是获得平滑进度条的可靠方法。它不依赖于值,但对于我们这些在 0-100 范围内的人来说,值本质上就是宽度。
    【解决方案5】:

    编写这个插件/方法可以非常轻松地为任何进度条设置动画,并且对我来说效果很好,所以我希望它也适用于其他所有人。

    (function( $ ) {
        $.fn.animate_progressbar = function(value,duration,easing,complete) {
            if (value == null)value = 0;
            if (duration == null)duration = 1000;
            if (easing == null)easing = 'swing';
            if (complete == null)complete = function(){};
            var progress = this.find('.ui-progressbar-value');
            progress.stop(true).animate({
                width: value + '%'
            },duration,easing,function(){
                if(value>=99.5){
                    progress.addClass('ui-corner-right');
                } else {
                    progress.removeClass('ui-corner-right');
                }
                complete();
            });
        }
    })( jQuery );
    

    只需将该代码添加到页面顶部的任何位置,然后在这样的元素上使用它

    $('#progressbar').animate_progressbar(value [, duration] [, easing] [, complete] );
    

    编辑:

    这是代码的缩小版,加载速度更快。

    (function(a){a.fn.animate_progressbar=function(d,e,f,b){if(d==null){d=0}if(e==null){e=1000}if(f==null){f="swing"}if(b==null){b=function(){}}var c=this.find(".ui-progressbar-value");c.stop(true).animate({width:d+"%"},e,f,function(){if(d>=99.5){c.addClass("ui-corner-right")}else{c.removeClass("ui-corner-right")}b()})}})(jQuery);
    

    【讨论】:

    • 请务必将进度条初始化为 0.01。当我将它设置为 0 时,它不起作用。
    • 真的吗?这很奇怪......这是我在我的网站上使用的代码,我从来没有遇到过任何问题,这就是我在这里发布它的原因
    • 我敢打赌这只是我安装的一部分。无论哪种方式,您的代码都运行良好,这就是我所要做的。因为你上面的答案,我想通了。
    • 嗯,这确实很奇怪,但感谢您的意见!如果它停止工作,我现在知道我可以尝试让它工作的第一件事
    【解决方案6】:

    这是一个优雅的解决方案:Growing Jquery Progress Bars

    $(function() {
    $("#progressbar").progressbar({
    value: 1
    });
    $("#progressbar > .ui-progressbar-value").animate({
    width: "37%"
    }, 500);
    });
    

    【讨论】:

      【解决方案7】:

      您可以使用简单的原生 html5 进度来完成。
      html:

      <progress id="score-progress-bar" value="10" max="100"></progress>
      

      js:

      $('#score-progress-bar').animate({value: your_value_from_0_to_100}, {duration: time_in_ms, complete: function(){console.log('done!');}});
      

      【讨论】:

        【解决方案8】:

        以下脚本不仅会为进度条设置动画,还会逐步增加/减少显示的 %values

                    // 'width' can be any number
        
                       $('#progressbar .ui-progressbar-value').animate(
                        {
                          width: width + '%'
                        },
                        {
                          duration: 3000,
                          step: function (now, fx) {
                              $('.leftvalue').html(parseInt(now) + '%');
                              $('.rightvalue').html((100 - parseInt(now)) + '%');
                          }
                        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-30
          • 1970-01-01
          • 2014-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多