0

我面临的问题是我无法弄清楚如何使动画条与百分比 I 同步。现在我看起来还可以,但是当我将值“43”更改为“100”时,百分比计数器变慢。

示例代码:

$(document).ready(function(){
                $(".barInner").animate({
                    width: 43 + "%",
                    opacity: 1
                }, 2500 );

                var display = $('.barInner');
                var currentValue = 0;
                var nextValue    = 43;

                var diff         = nextValue - currentValue;
                var step         = ( 0 < diff ? 1 : -1 ); 

                for (var i = 0; i < Math.abs(diff); ++i) {
                    setTimeout(function() {
                        currentValue += step
                        display.text(currentValue + "%");
                    }, 54 * i)   
                }
            });

http://jsfiddle.net/vTKw7/

提前致谢,

缺口

4

1 回答 1

2

You can't synchronize an .animate() and a setTimeout() the way you seem to want. Try using your setTimeout to control the width of the progress bar and display the percentage number at the same time.

        $(document).ready(function () {

            var display = $('.barInner');
            var currentValue = 0;
            var nextValue = 100;
            var diff = nextValue - currentValue;
            var step = (0 < diff ? 1 : -1);

            for (var i = 0; i < Math.abs(diff); ++i) {
                setTimeout(function () {
                    currentValue += step;
                    display.text(currentValue + "%");
                    $(".barInner").css({
                        width: currentValue + "%",
                        opacity: 1
                    });
                }, 54 * i)
            }
        });

http://jsfiddle.net/mblase75/6TMWm/

Or, using .stop().animate() to smooth it out a little:

                    $(".barInner").stop().animate({
                        width: currentValue + "%",
                        opacity: 1
                    },50);

http://jsfiddle.net/mblase75/6TMWm/1/

于 2013-11-05T14:10:54.203 回答