jquery - 进度条应显示在屏幕上,直到网页完全加载

标签 jquery load progress-bar page-load-time

我正在使用以下代码来显示进度条。

jQuery('#container').showLoading(
{
      'afterShow':
       function () 
       {
          setTimeout("jQuery('#container').hideLoading()", 1000);
       }
});

进度条在屏幕上显示直到 1000 毫秒,然后消失。 但我希望进度条显示在屏幕上,直到(时间)=页面加载。 意味着静态地我需要 1000 毫秒,但我希望进度条显示那么多时间.. 页面需要时间加载。

那么如何获取页面加载时间并将其传递到这里?

最佳答案

我们需要做的是首先尽快显示进度指示器,然后在页面完全加载时隐藏它。我们可以通过在 DOM 准备好时将加载事件绑定(bind)到窗口对象来做到这一点。

请注意:在这么小的页面上,您可能不会注意到加载程序,因为这一切都发生得太快了。

jQuery(document).ready(function() {

  showHideLoading(); // load this func as soon as the doc is ready

});

function showHideLoading() {

  // We'll be referencing #loader more than once so let's cache it
  var loader = jQuery("#loader");

  // now we'll show the #loader
  loader.show();

  // We'll set up a new event so that when everything on the page
  // has finished loading, we hide our #loader
  jQuery(window).on("load", function() {
    loader.fadeOut(500);
  });
}
body {
  margin: 0;
  }
img {
  max-width: 100%;
  /*this is only here to make the page load a little more slowly */
}
#loader {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);

}
#loader div {
  width: 150px;
  height: 80px;
  margin: -40px 0 0 -75px;
  background: #fff;
    position: absolute;
  top: 50%;
  left: 50%;
  text-align: center;
  border-radius: 50%;
  line-height: 80px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- this is only here to make the page load a little more slowly -->
<img src="http://lorempixel.com/image_output/cats-q-c-1920-1080-8.jpg" />


<div id="loader">

  <div>Loading</div>

</div>

关于jquery - 进度条应显示在屏幕上,直到网页完全加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15918444/

相关文章:

javascript - 为什么 jQuery 文件上传与 KnockoutJS 一起使用时需要重新初始化?

javascript - 如果里面没有 <img> 内容,则隐藏 <dl>

Android webview loadDataWithBaseURL 如何从 Assets 加载图像?

javascript - 有人可以调试我的 Javascript 小程序中的进度条问题吗?

java - 有没有办法显示另一个类中函数的进度?

Javascript、CSS、jQuery - 我的 Canvas 移动到了不该移动的地方

javascript - 模板文字适用于除 IE 之外的其他浏览器

php - 允许页面仅在 iframe 中加载

jquery - 使用jquery将xml文件内容加载到div中

excel - 如何从文本框中删除光标(在进度条中)?