如何在加载页面时显示正在运行的进度条

ami*_*pta 45 javascript css jquery html5-canvas

我想说明一个正在运行的进度条,而我的页面加载喜欢这里,在我的网页.我在我的示例中使用了一个简单的加载图像,但我想在正在运行的进度条中将其转换.这是我的代码:

$(window).load(function() {
    alert("hi");
    $('#loading').hide();
});
Run Code Online (Sandbox Code Playgroud)
#loading {
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    position: fixed;
    display: block;
    opacity: 0.7;
    background-color: #fff;
    z-index: 99;
    text-align: center;
}

#loading-image {
    position: absolute;
    top: 100px;
    left: 240px;
    z-index: 100;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id="loading">
    <img id="loading-image" src="http://cdn.nirmaltv.com/images/generatorphp-thumb.gif" alt="Loading..." />
</div>
<div id="txt">
    <h2>Let AJAX change this text</h2>
</div>
<button>Change Content</button>
Run Code Online (Sandbox Code Playgroud)

这是一个JSFiddle

Tap*_*Pal 13

我已从此页面复制了以下相关代码.希望这可能对你有所帮助.

$.ajax({
  xhr: function() {
    var xhr = new window.XMLHttpRequest();
    //Upload progress
    xhr.upload.addEventListener("progress", function(evt) {
      if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        //Do something with upload progress
        console.log(percentComplete);
      }
    }, false);
    //Download progress
    xhr.addEventListener("progress", function(evt) {
      if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        //Do something with download progress
        console.log(percentComplete);
      }
    }, false);
    return xhr;
  },
  type: 'POST',
  url: "/",
  data: {},
  success: function(data) {
    //Do something success-ish
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 这不会以任何方式回答这个问题.OP正在寻找显示页面加载进度的进度条.此答案处理上传进度.上传!=下载. (7认同)
  • 你们不能读懂吗?BOTH上传和下载有进度更新. (7认同)
  • OP 要求提供页面加载进度条。您提供了一种确定上传进度的方法。 (4认同)
  • +1你回答了这个问题最困难的部分 - 确定进度的百分比."简单"部分(实际进度条)可以通过多种方式回答,包括jQueryUI进度条:http://jqueryui.com/progressbar/ (4认同)

Mar*_*ean 6

这是一个鸡蛋问题.您将无法执行此操作,因为您需要加载资源以显示进度条小部件,此时您的页面将被完全或部分下载.此外,您需要知道用户请求之前页面的总大小,以便计算百分比.

这比它的价值更麻烦.

  • 我不是贬低者,而是看看我的情景; `index.html`首先发送到客户端,其中已经有jQuery,内联 - 文件就绪`$(function(){/*here*/})`我想抓一个巨大的`.js`文件1MB,因此有意义地向用户显示进度条! (5认同)
  • 这是您在网络上很少见到的答案,您无法采取任何措施来测量http上的网页总体大小,而试图使其真实化只是效率低下,大多数开发人员只是产生了加载的错觉(通过测量加载方式平均而言,他们的页面需要加载,并且使条形停靠不到100%,直到完全加载为止)。这需要在网络上更公开地讨论。 (2认同)

Omk*_*xit 6

简单的步骤,请遵循它们,我想它将解决您的问题

在页面中包含这些CSS,

.progress {
      position: relative;
      height: 2px;
      display: block;
      width: 100%;
      background-color: white;
      border-radius: 2px;
      background-clip: padding-box;
      /*margin: 0.5rem 0 1rem 0;*/
      overflow: hidden;

    }
    .progress .indeterminate {
background-color:black; }
    .progress .indeterminate:before {
      content: '';
      position: absolute;
      background-color: #2C67B1;
      top: 0;
      left: 0;
      bottom: 0;
      will-change: left, right;
      -webkit-animation: indeterminate 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite;
              animation: indeterminate 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite; }
    .progress .indeterminate:after {
      content: '';
      position: absolute;
      background-color: #2C67B1;
      top: 0;
      left: 0;
      bottom: 0;
      will-change: left, right;
      -webkit-animation: indeterminate-short 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) infinite;
              animation: indeterminate-short 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) infinite;
      -webkit-animation-delay: 1.15s;
              animation-delay: 1.15s; }

    @-webkit-keyframes indeterminate {
      0% {
        left: -35%;
        right: 100%; }
      60% {
        left: 100%;
        right: -90%; }
      100% {
        left: 100%;
        right: -90%; } }
    @keyframes indeterminate {
      0% {
        left: -35%;
        right: 100%; }
      60% {
        left: 100%;
        right: -90%; }
      100% {
        left: 100%;
        right: -90%; } }
    @-webkit-keyframes indeterminate-short {
      0% {
        left: -200%;
        right: 100%; }
      60% {
        left: 107%;
        right: -8%; }
      100% {
        left: 107%;
        right: -8%; } }
    @keyframes indeterminate-short {
      0% {
        left: -200%;
        right: 100%; }
      60% {
        left: 107%;
        right: -8%; }
      100% {
        left: 107%;
        right: -8%; } }
Run Code Online (Sandbox Code Playgroud)

然后在进度条中添加您的身体标签,

<div class="progress" id="PreLoaderBar">
        <div class="indeterminate"></div>
    </div>
Run Code Online (Sandbox Code Playgroud)

然后它将在您的页面加载时开始,现在您所要做的就是在页面加载时将其隐藏,或者将可见性设置为none,或者使用javascript将其隐藏,

document.onreadystatechange = function () {
            if (document.readyState === "complete") {
                console.log(document.readyState);
                document.getElementById("PreLoaderBar").style.display = "none";
            }
        }
Run Code Online (Sandbox Code Playgroud)

让我知道您是否遇到任何问题,也可以添加任何类型的进度条,您可以轻松地找到它们,在此示例中,我使用了不确定的进度条。

  • 很棒的答案! (2认同)