Jquery/Ajax loader 的时间取决于页面的加载时间



情况:
我正在寻找一个 jquery/ajax 加载器,发现这个线程很有帮助。
现在我用这个小提琴作为我的加载器。
一切正常,但我只有一个担忧。
在js代码下方,"响应时间"固定为2.5秒。

$.mockjax({ url: "/mockjax", responseTime: 2500 });

因此,如果我的页面加载时间超过 5 秒,加载程序只会以 2.5 秒的速度运行。

问题:
我将如何根据加载器的时间与页面不可预测的加载时间相依存?

非常感谢您的帮助。谢谢!

您可以显示加载器 gif,直到窗口完全加载

$(window).load(function(){
}

并像这样做:

$(window).load(function(){
    $('#cover').fadeOut(1000); 
});
#cover {
    background: url("http://www.aveva.com/Images/ajax-loader.gif") no-repeat scroll center center #FFF;
    position: absolute;
    height: 100%;
    width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="cover"></div>
<img src="https://i.ytimg.com/vi/lklDJ5VRQao/maxresdefault.jpg" />

它将从显示 gif 开始,并在内容完全加载后淡出。


我从这个网站上找到了我正在寻找的东西。

下面是我使用的代码。

$(document).ready(function(){
    $("#spinner").bind("ajaxSend", function() {
        $(this).show();
    }).bind("ajaxStop", function() {
        $(this).hide();
    }).bind("ajaxError", function() {
        $(this).hide();
    });
 
    $('#button-upload').click(function() {
        $('#spinner').show();
    });
 });
.spinner {
    position: fixed;
    top: 50%;
    left: 50%;
    margin-left: -50px;
    margin-top: -50px;
    text-align:center;
    z-index:1234;
    overflow: auto;
    width: 100px;
    height: 102px; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="spinner" class="spinner" style="display:none;">
    <img id="img-spinner" src="http://sampsonresume.com/labs/pIkfp.gif" alt="Loading"/>
</div>
<input type = "submit" value = "Upload" id="button-upload" />

加载程序在此代码段中是不确定的,但它在我的网站上运行良好。
无论如何,谢谢大家的帮助=(

最新更新