如何使用Jquery简单地将数字设置为某个百分比?

时间:2014-08-23 06:22:48

标签: jquery jquery-animate percentage

我不想要进度条,我只想将数字设置为某个百分比

$('#animate_percent').click(function() {
//How the h#$%^ is this *%&&^*
}); 


<!--Animate 20% to 50%-->
<span class="tabs_percent">20%</span>

1 个答案:

答案 0 :(得分:2)

您可以使用setInterval()

var counter;

$('#animate_percent').on('click', function(e) { 
    e.preventDefault();
    var $percent = $('.tabs_percent'),
        curr = parseInt($percent.text()),
        to = 50;

    counter = window.setInterval(function() {  
        if(curr <= to)
        {
            $percent.text((curr++)+'%');
        }
        else
        {
            window.clearInterval(counter);
        }
    }, 20);    
});

jsFiddle Demo