javascript - 如何在页面加载时显示运行进度条

标签 javascript jquery css html5-canvas

我想在我的页面加载时显示一个运行进度条 like here , 在我的页面中。 我在我的示例中使用了一个简单的加载图像,但我想在运行进度条中转换它。这是我的代码:

$(window).load(function() {
    alert("hi");
    $('#loading').hide();
});
#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;
}
<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>

这是一个JSFiddle

最佳答案

我从This page复制了下面的相关代码.希望这对您有所帮助。

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

关于javascript - 如何在页面加载时显示运行进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18981909/

相关文章:

javascript - 包含 Javascript 的样式并包含非 Javascript 的其他样式

CSS 三 Angular 形和 IMG

javascript - 使用 MailApp.sendEmail 时发生服务器错误

javascript - 用双引号替换 JSON 对象值单引号

jquery - css:类更改后第一个类型未正确处理(由猫头鹰旋转木马)

javascript - 将 json 数据传递给 javascript 变量

css - 使用CSS3,我们应该如何使用新单位vh、vw,并考虑到body margin?

javascript - 使用 javascript 的类型而不是其 id 来模拟按钮单击

javascript - 在 Javascript 中使用正则表达式检查模式

javascript - jQuery 字段验证 - 是否为空,以及正确的值?