你如何设置jQuery UI进度条的值的动画?

时间:2011-02-18 23:04:44

标签: jquery jquery-ui scrollbar jquery-animate

我已经设置了jQuery UI进度条,但是无法使用jQuery动画来设置它的值的动画。关于如何使这项工作的任何想法?

percentDone变量保存一个0到100之间的数字,显示滚动条应该有多远(这样可以正常工作)。

我尝试过几种不同的东西但无济于事。这是我到目前为止所做的:

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

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

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

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

8 个答案:

答案 0 :(得分:59)

$(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应用程序会告诉您文件大小,并在您达到所需的最大值时通知您,所以我的第一个代码是旨在展示进度条设定器和吸气剂的使用,当然还有什么使整个过程发挥作用,例如循环;第二个是对糖的相同概念的改编。

答案 1 :(得分:41)

CSS3动画

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

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

您可以详细了解CSS3 Transitions

答案 2 :(得分:34)

以下是如何让它平滑动画,而不是在@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')

答案 3 :(得分:11)

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

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

应该初始化进度条,让我们说0.1工作

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

答案 4 :(得分:8)

写这个插件/方法非常容易为任何进度条制作动画,对我来说效果非常好,所以我希望它对其他人也有效。

(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);

答案 5 :(得分:3)

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

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

答案 6 :(得分:1)

以下脚本不仅可以动画进度条,还可以逐步增加/减少显示的%值

            // '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)) + '%');
                  }
                });

答案 7 :(得分:1)

你可以用简单的原生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!');}});