首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在页面加载时显示正在运行的进度条

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

Stack Overflow用户
提问于 2013-09-24 20:41:39
回答 5查看 218.1K关注 0票数 53

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

代码语言:javascript
复制
$(window).load(function() {
    alert("hi");
    $('#loading').hide();
});
代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
<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

EN

回答 5

Stack Overflow用户

发布于 2013-09-24 21:02:39

我已经从This page复制了下面的相关代码。希望这能对你有所帮助。

代码语言:javascript
复制
$.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
  }
});
票数 14
EN

Stack Overflow用户

发布于 2013-09-24 21:05:28

这是一个先有鸡后有蛋的问题。您不能这样做,因为您需要加载资源来显示进度条小部件,此时您的页面将被完全或部分下载。此外,在用户请求之前,您需要知道页面的总大小,以便计算百分比。

这比它的价值更麻烦。

票数 8
EN

Stack Overflow用户

发布于 2017-06-23 14:59:42

简单的步骤,遵循它们,我想它会解决你的问题

在您页面中包含这些Css,

代码语言:javascript
复制
.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%; } }

然后将进度栏包含在body标记中,

代码语言:javascript
复制
<div class="progress" id="PreLoaderBar">
        <div class="indeterminate"></div>
    </div>

然后它将在页面加载时启动,现在您要做的就是在页面加载时隐藏它,或者使用javascript将可见性设置为无或隐藏。

代码语言:javascript
复制
document.onreadystatechange = function () {
            if (document.readyState === "complete") {
                console.log(document.readyState);
                document.getElementById("PreLoaderBar").style.display = "none";
            }
        }

如果你遇到任何问题,请让我知道,而且,你可以添加任何类型的进度条,你可以很容易地找到它们,对于这个例子,我使用了一个不确定的进度条。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18981909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档