jquery页面加载延迟
我想在页面加载一段时间后从外部源加载 div。
这是我的 jquery 脚本。发生的情况是先加载内容,然后加载 loader.gif。如何在 xx 时间后显示来自外部源的内容?我对 .load() 函数有一个想法,但不知道如何实现。
$(window).load(function() {
$('<div id="overlay"><img src="loading.gif" /></div>')
.css('opacity', '0.5')
.insertAfter('body');
window.setTimeout(function() {
$(document).ready(function() {
$('#overlay').remove();
});}, 1000);
$('.body').show();
});
<div id="container">
<div id="header">navigation</div>
<div id="body" class="body">Body</div>
<div id="footer">footer</div>
</div>
#overlay {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background: gray;
}
.body{display:none;}
#overlay img {
display: block;
width: 32px;
height: 32px;
position: absolute;
top: 50%;
left: 50%;
margin: -26px 0 0 -26px;
padding: 10px;
background: #fff;
-moz-border-radius: 6px;
border-radius: 6px;
}
I want to load a div from external source after page has loaded with some delay.
Here is the jquery script I have. What is happening is content is loading first and then loader.gif later. How can I show content from an external source after some xx time..? I have an idea about .load()
function but not sure how to implement.
$(window).load(function() {
$('<div id="overlay"><img src="loading.gif" /></div>')
.css('opacity', '0.5')
.insertAfter('body');
window.setTimeout(function() {
$(document).ready(function() {
$('#overlay').remove();
});}, 1000);
$('.body').show();
});
<div id="container">
<div id="header">navigation</div>
<div id="body" class="body">Body</div>
<div id="footer">footer</div>
</div>
#overlay {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background: gray;
}
.body{display:none;}
#overlay img {
display: block;
width: 32px;
height: 32px;
position: absolute;
top: 50%;
left: 50%;
margin: -26px 0 0 -26px;
padding: 10px;
background: #fff;
-moz-border-radius: 6px;
border-radius: 6px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说 gif 是在你 ajax pull 之后加载的。如果这是您的问题,您可以在加载 gif 后调用 ajax pull,方法是:
You said that the gif is being loaded after you ajax pull. If that is your problem, you can call your ajax pull after the gif is loaded, by doing:
来自 jquery 网站
您需要在正文加载时显示 div 并在加载时隐藏它。我不确定这是否是你要问的。如果它不是 setTimeout 函数,则可能是您的问题想。
From the jquery website
You need to show your div while the body is loading and hide it onload. I'm not sure if that is what you're asking though. If it's not the setTimeout function might be what you want.
您的代码中有一个小问题:
在此代码中,您为执行此操作的函数设置了超时:
基本上它会等待一秒钟,然后向文档的就绪事件添加一个侦听器,并在该侦听器中执行实际的工作。因此,如果在第一秒内触发了就绪事件,该函数将根本不会执行。
您的代码应如下所示:
占位符将在加载事件后一秒内被删除。请注意,“加载”和“就绪”事件是不同的。并且“load”并不能保证 DOM 会被解析或渲染。
There's a small problem in your code:
In this code you set the timeout, to the function that does this:
Basically it waits a second, and then adds a listener to document's ready event and in that listener it performs the actual work. So in case if ready event was fired during the first second - the function will not execute at all.
Your code should look like this:
The placeholder will be removed in a second after the load event. Note that "load" and "ready" events are different. And "load" doesn't guarantee that DOM will be parsed or rendered.