HTML5进度条动画

时间:2012-06-18 07:44:25

标签: html5 progress-bar

我在我的应用中使用HTML5进度条。我想知道是否有任何方法可以控制进度条的动画速度。我想在一定的时间间隔后显示进度,我使用javascript的setTimeout方法,以便在屏幕渲染后设置值。但动画太快了。有没有办法控制它?

感谢。

3 个答案:

答案 0 :(得分:5)

我不确定我理解“动画”是什么意思,但这里是一个在控制进展速度的同时使用进度条的例子:http://jsfiddle.net/526hM/

HTML:

<progress max="200" value="1"></progress>
<div id="val"></div>

脚本:

$(document).ready(function(){
    var interval = 2, //How much to increase the progressbar per frame
        updatesPerSecond = 1000/60, //Set the nr of updates per second (fps)
        progress =  $('progress'),
        animator = function(){
            progress.val(progress.val()+interval);
            $('#val').text(progress.val());
            if ( progress.val()+interval < progress.attr('max')){
               setTimeout(animator, updatesPerSecond);
            } else { 
                $('#val').text('Done');
                progress.val(progress.attr('max'));
            }
        }

    setTimeout(animator, updatesPerSecond);
});

答案 1 :(得分:2)

这是一个例子。 JavaScript函数:

window.onload = addTenPercent();

function addTenPercent() {
    var bar = document.getElementById("progressBar");
    setInterval(addTenPercent, 100);
    bar.value += 5;
};

HTML:

<progress id="progressBar" max="100" value="0"></progress>

答案 2 :(得分:0)

曼塔斯&#39;回答上面看起来很好并且有效(不必使用jQuery),但我想知道我们是否也可以使用setTimeout函数?在100次迭代后退出的递归函数中的setTimeout是否也有效?复制Mantas&#39;上面的代码并稍微改变一下:

JavaScript的:

function onWindowLoad(){ // to be called by onload event
    var bar = document.getElementById("progressBar");
    addOne();
}

function addOne() {
    if (bar.value < 100) { // this is the max value of the bar and the # of iterations
        setTimeout(addOne, 80); // this literal value controls the speed
        bar.value += 1;    
    }else{  
        return;
    } 
}

HTML:

<progress id="progressBar" max="100" value="0"></progress>