how to create a jQuery loading bar? (like the ones used on flash sites)

我是研究僧i 提交于 2019-11-29 15:14:10

You can use the Progress Bar from jQuery UI

Upon loading of each element simply change the value of the progress bar.

$.when($.ajax("video1.ogv"))
.then(function () {
    videoLoaded[1] = true;
    updateProgressBar();
});

$.when($.ajax("video2.ogv"))
.then(function () {
    videoLoaded[2] = true;
    updateProgressBar();
});

$.when($.ajax("video3.ogv"))
.then(function () {
    videoLoaded[3] = true;
    updateProgressBar();
});

var updateProgressBar = function() {
    // iterate though the videoLoaded array and find the percentage of completed tasks
    $("#progressbar").progressbar("value", percentage);
}

The jQueryUI Progressbar is a powerful API to create custom loading bars. You can couple that with the jQuery Deferred API to do something like

var ProgressBar = {
    advance : function(percent){
        return $.Deferred(function(dfr){
            $(‘.progressbar’).animate({width:percent + ‘%’}, dfr.resolve);
        }).promise();
    }
};

ProgressBar.advance(86).then(function(){
    //do something neat
});

(via http://www.decipherinc.com/n/blog/development-and-engineering-team/2011/06/working-jquery-s-deferred-0)

With jQuery-ui it shouldn't be that difficult. If you know the total number of elements you have to wait for, you can just create it and update the progress bar: http://jsfiddle.net/melachel/4mh7Q/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!