最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

I have multiple elements I need to load before the user has control over the page (mostly images, videos, and audio). Currently, I'm loading them with the $.when() function, like this:

//all elements are hidden, except for a black background and
//a "loading" gif in the middle.

$.when($.ajax("video1.ogv"), $.ajax("video2.ogv"), $.ajax("videoN.ogv"))
.then(function () {
   //show the site with all the content preloaded...
});

Is there any way to create a loading bar that shows the progress (in percentage) of all the elements loading in the background? Like what happens in most flash sites with heavy media, for example: /

Can it be done purely with jQuery or Javascript?

Thanks in advance!

I have multiple elements I need to load before the user has control over the page (mostly images, videos, and audio). Currently, I'm loading them with the $.when() function, like this:

//all elements are hidden, except for a black background and
//a "loading" gif in the middle.

$.when($.ajax("video1.ogv"), $.ajax("video2.ogv"), $.ajax("videoN.ogv"))
.then(function () {
   //show the site with all the content preloaded...
});

Is there any way to create a loading bar that shows the progress (in percentage) of all the elements loading in the background? Like what happens in most flash sites with heavy media, for example: http://www.saizenmedia./nightwishsite/

Can it be done purely with jQuery or Javascript?

Thanks in advance!

Share Improve this question edited Aug 17, 2012 at 21:26 hippietrail 17k21 gold badges109 silver badges179 bronze badges asked Dec 9, 2011 at 7:30 CCrawlerCCrawler 6493 gold badges11 silver badges24 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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 pleted 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./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/melachel/4mh7Q/

发布评论

评论列表(0)

  1. 暂无评论