我正在使用以下代码来显示进度条。
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/