简体   繁体   English

如何在页面加载完成之前显示页面加载html文件?

[英]How to show Page Loading html file until the page has finished loading?

I need a different page loader file (like: loader.html ) in which i have only loader.gif image with some css styling not in the same index.html file. 我需要一个不同的页面加载器文件(如: loader.html ),其中我只有loader.gif图像,其中一些css样式不在同一个index.html文件中。 So now i want to load my index.html file before it loads i want to show my loader.html file and if my all content loaded hide the loader.html file and show index.html file content. 所以现在我想在加载之前加载我的index.html文件我想显示我的loader.html文件,如果我加载了所有内容,则隐藏loader.html文件并显示index.html文件内容。 Hope you understand what i am saying please help :) 希望你理解我在说什么,请帮助:)

I tried this Code: 我试过这个代码:

$(window).load(function(){
    if($("body").load("loader.html").fadeOut(5000)){
        $("loader.html").hide();
        $("index.html").show();
    }
});

You can do this: 你可以这样做:

  1. On Page Load show your .gif or loader .html in some <div> as you required. 在页面加载中,根据需要在某些<div>显示.gif或加载器.html
  2. Make ajax call to load html. 进行ajax调用以加载html。
  3. After complete ajax call hide/remove . 完成ajax之后,调用hide / remove。 gif/loader.html and show your index.html . gif/loader.html并显示你的index.html

This might solve your problem. 这可能会解决您的问题。

  1. try this instead On document.load function() show the loader . 请改用此方法。在document.load function()中显示loader。 On document.ready function() hide the loader` 在document.ready function()上隐藏加载程序`

.load( url [, data ] [, complete ] ) $.load .load(url [,data] [,complete]) $ .load

$(window).load(function(){
  $("body").load("loader.html", function(){
    /* place code need to executed after loader.html load */
  });
});

jQuery jQuery的

$(document).ready(function () {
    //for demo, I used timer for 3 second
    var timer = window.setTimeout(requestPage, 3000);

    function requestPage(){
        sendRequest("index.php", function(sResponse){
            clearTimeout(timer);
            //clear everything everything in body HTML
            //change inner html with sResponse
            $("body").children().fadeOut(300, function(){
                $(this).remove();
                $("body").html(sResponse)
            });
        });
     }

    function sendRequest(url, oncomplete){
       $.ajax({
         url: url,
         type: 'POST',
         dataType: 'html',
         cache: false,
         timeout: 60000,
         complete: function(e, xhr, opt) {
            if(typeof oncomplete === "function") oncomplete(e.responseText);
          }
       });
    }
});

HTML HTML

<div class="loader"><img src="loader.gif" /> <em>Loading page. Please wait...</em></div>

Hope you can help 希望你能帮忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM