如何在页面完成加载之前显示页面加载 html 文件
How to show Page Loading html file until the page has finished loading?
我需要不同的页面加载器文件(如:loader.html
),其中我只有loader.gif
图像,其中一些 css 样式不在同一个index.html
文件中。所以现在我想在加载之前加载我的index.html
文件,我想显示我的loader.html
文件,如果我加载了所有内容,则隐藏loader.html
文件并显示index.html
文件内容。希望你明白我在说什么,请帮助:)
我试过这个代码:
$(window).load(function(){
if($("body").load("loader.html").fadeOut(5000)){
$("loader.html").hide();
$("index.html").show();
}
});
.load( url [, data ] [, complete ] ) $.load
$(window).load(function(){
$("body").load("loader.html", function(){
/* place code need to executed after loader.html load */
});
});
你可以这样做:
- 在页面加载时,根据需要在某些
<div>
中显示您的.gif
或加载程序.html
。 - 进行 ajax 调用以加载 html。
- 完成后 ajax 调用隐藏/删除 .
gif/loader.html
并展示您的index.html
.
这可能会解决您的问题。
- 试试这个在 document.load 函数() 上显示加载器。在 document.ready 函数() 上隐藏加载器
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
<div class="loader"><img src="loader.gif" /> <em>Loading page. Please wait...</em></div>
希望你能帮到你
相关文章:
- 加载文件,然后调用回调函数
- 根据浏览器大小加载文件
- 错误:无法在“XMLHttpRequest”上执行“发送”:无法加载“文件”:AngularJS SPA
- Ajax在mootools中使用来加载文件
- 加载文件时xmlhttp不稳定,如何修复
- Require.js何时加载文件
- 有没有办法在不打包的情况下在 nodejs 上加载文件
- 如何通过从浏览器 js 控制台触发的 require.js 重新加载文件
- 使用jQuery加载文件,计算类的元素并设置间隔比率
- 如何在 AngularJS UI 路由器中从bower_component加载文件
- JavaScript 本地应用程序保存/加载文件
- 如何在特定页面上加载文件
- 内容安全策略不从本地主机加载文件
- Javascript 无法从其他目录加载文件
- Extjs MVC 文件夹结构无法加载文件
- 节点.js - 在运行时加载文件
- nodejs加载文件
- Javascript/AAJAX仅重新加载文件“;onchange”;
- 加载文件,并在特定行之后删除javascript/jquery中的所有剩余行
- 是否可以在非服务页面上使用JS/HTML5 FileReader加载文件