简体   繁体   English

构建进度条以在显示之前加载所有内容

[英]Building a Progress bar for Loading everything before display

Hello i really try and search before ask.. Im trying to use jquery for a progress bar, i found a lot of info, the problem is most of the examples are just "animations" i mean, they dont really display the %, of the loading process.. and i cant find a way to do it. 您好,我真的尝试过搜索,然后问..我试图将jquery用于进度条,我发现了很多信息,问题是大多数示例只是“动画”,我的意思是,它们并没有真正显示%加载过程..我找不到一种方法。

Im trying to use this http://www.htmldrive.net/items/show/791/Very-Beautiful-CSS3-And-JQuery-progress-bar 我正在尝试使用此http://www.htmldrive.net/items/show/791/Very-Beautiful-CSS3-And-JQuery-progress-bar

Mixed With the correct answer on this question. 与这个问题的正确答案混合在一起。 how to create a jQuery loading bar? 如何创建一个jQuery加载栏? (like the ones used on flash sites) (例如Flash网站上使用的网站)

But i just can´t manage to merge 但我只是无法合并

$.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);
}

So the bar on CSS that i show you .. really display the % that has been loading of the content of #main_content or a video like in example.. 因此,我向您展示的CSS栏实际上显示了已加载#main_content或示例中的视频内容的百分比。

Hope i make my self clear because my english its very bad. 希望我能说清楚一点,因为我的英语不好。

Why not do something like this? 为什么不这样做呢? Just change it for AJAX and use success as your trigger. 只需将其更改为AJAX,然后将success用作触发条件即可。 Basically a never ending animated gif while waiting for you data loading. 在等待数据加载时,基本上是一个永无休止的动画gif。

<div id="chartDiv"></div>

var chartDiv = document.getElementById("chartDiv");
var loadingImg = document.createElement("img");
var newImg = document.createElement("img");

loadingImg.src = "http://i1267.photobucket.com/albums/jj559/rizkytanjo96/loading.gif";
chartDiv.appendChild(loadingImg);

function placeLoaded() {
    chartDiv.removeChild(loadingImg);
    chartDiv.appendChild(newImg);
}

function getNew() {
    newImg.addEventListener("load", placeLoaded, false);
    newImg.src = "http://i1276.photobucket.com/albums/y462/staffpicks/Animated_GIFs/ahhhh.gif";
}

setTimeout(getNew, 10000);

on jsfiddle jsfiddle上

Alternatively, if you don't want an animated gif, you could do something like this with javascript. 另外,如果您不需要动画的gif,则可以使用javascript执行类似的操作。

#pwidget {
    background-color:lightgray;
    width:254px;
    padding:2px;
    -moz-border-radius:3px;
    border-radius:3px;
    text-align:left;
    border:1px solid gray;
}
#progressbar {
    width:250px;
    padding:1px;
    background-color:white;
    border:1px solid black;
    height:28px;
}
#indicator {
    width:0px;
    background-image:url("http://img827.imageshack.us/img827/269/shadedgreen.png");
    height:28px;
    margin:0;
}
#loading {
    text-align:center;
    width:250px;
}

var pwidget = {
    maxprogress: 250,
    actualprogress: 0,
    itv: 0,
    delay: 10,
    prog: function () {
        if (pwidget.actualprogress >= pwidget.maxprogress) {
            clearInterval(pwidget.itv);
            return;
        }

        var indicator = document.getElementById("indicator");

        pwidget.actualprogress += 1;
        indicator.style.width = pwidget.actualprogress + "px";

        if (pwidget.actualprogress === pwidget.maxprogress) {
            pwidget.restart();
        }
    },
    start: function () {
        pwidget.itv = setInterval(pwidget.prog, pwidget.delay);
    },
    stop: function () {
        clearInterval(pwidget.itv);
    },
    restart: function () {
        pwidget.actualprogress = 0;
        pwidget.stop();
        pwidget.start();
    },
    element: function () {
        var pwidget = document.createElement("div"),
            progressbar = document.createElement("div"),
            indicator = document.createElement("div"),
            loading = document.createElement("div");

        pwidget.id = "pwidget";
        progressbar.id = "progressbar";
        indicator.id = "indicator";
        loading.id = "loading";
        loading.textContent = "Loading ...";

        progressbar.appendChild(indicator);
        pwidget.appendChild(progressbar);
        pwidget.appendChild(loading);

        return pwidget;
    }
};

var chartDiv = document.getElementById("chartDiv");
var widget = pwidget.element();
var newImg = document.createElement("img");

function placeLoaded() {
    pwidget.stop();
    chartDiv.removeChild(widget);
    chartDiv.appendChild(newImg);
}

function getNew() {
    newImg.addEventListener("load", placeLoaded, false);
    newImg.src = "http://i1276.photobucket.com/albums/y462/staffpicks/Animated_GIFs/ahhhh.gif";
}

chartDiv.appendChild(widget);
pwidget.start();
setTimeout(getNew, 10000);

on jsfiddle jsfiddle上

Or jQueryUI has a progress bar and you could do a similar thing to my example above. 或者jQueryUI有一个进度条 ,您可以做与上面的示例类似的操作。

Part of the reason most sites have moved to a loading gif over a percent is because the percent is really unreliable. 大多数网站移至加载gif百分比以上的部分原因是因为该百分比确实不可靠。

You could possibly be in a situation where you are showing the actual progress as an exact percent then something happens causing traffic to slow down and frustrates the user when it take 5s to load 75% and then 10s to load 25%. 您可能会遇到这样的情况,即您将实际进度显示为准确的百分比,然后发生某些事情,导致流量减慢,并且在5秒内加载75%的时间然后10秒内加载25%的时间使用户感到沮丧。 Tere are plenty of examples but I will stop here. Tere有很多例子,但我会在这里停止。

If it is at all possible I would take a loading approach over a percent don't approach for this reason alone. 如果有可能的话,我会采用一个百分比以上的加载方法,不要仅仅因为这个原因就不要这样做。 As long as there is an indication of work being done users are generally happy. 只要有完成工作的迹象,用户通常都会感到满意。

[EDIT] sorry it took me a little bit to get back to this and give you an example. [EDIT]很抱歉,我花了点时间回到这个问题上,并举一个例子。 this is really basic and really depends on how you are showing this loading bar as to how you will add it to the page. 这是非常基本的操作,实际上取决于您如何显示此加载栏以及如何将其添加到页面。 Focus is on this line 重点在这条线上

$('<div>').progressbar({ value: false })

hope this helps. 希望这可以帮助。 The jQuery progress bar does allow for percentage, still i would try to avoid it unless its absolutely required. jQuery进度栏确实允许百分比,但除非绝对必要,否则我还是会尽量避免使用百分比。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-2.0.0.js"></script>
    <script src="Scripts/jquery-ui-1.10.3.js"></script>
    <link href="Content/themes/base/jquery-ui.css" rel="stylesheet" />
    <script>
        function show_loading()
        {
            $('<div>').dialog
                ({
                    title: 'loading...',
                    modal: true,
                    closeOnEscape: true,
                    draggable: false,
                    resizable: false
                })
            .append($('<div>').progressbar({ value: false }));
        }
    </script>
</head>
<body>
    <button id="show_loading" onclick="show_loading();">show loader</button>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM