jquery页面加载延迟

发布于 2024-10-22 03:56:53 字数 941 浏览 1 评论 0原文

我想在页面加载一段时间后从外部源加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ㄟ。诗瑗 2024-10-29 03:56:53

你说 gif 是在你 ajax pull 之后加载的。如果这是您的问题,您可以在加载 gif 后调用 ajax pull,方法是:

yourimg = new Image(); 
yourimg.src = "loading.gif";

yourimg.loadEvent = function(url, image){
    var overlay = $('<div id="overlay" />');
    overlay.append(image).css('opacity', '0.5').insertAfter('body');
    //call whatever you want here. your image is loaded.
}

yourimg.load();

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:

yourimg = new Image(); 
yourimg.src = "loading.gif";

yourimg.loadEvent = function(url, image){
    var overlay = $('<div id="overlay" />');
    overlay.append(image).css('opacity', '0.5').insertAfter('body');
    //call whatever you want here. your image is loaded.
}

yourimg.load();
各自安好 2024-10-29 03:56:53

来自 jquery 网站

The load event is sent to an element when it and all sub-elements have been
completely loaded. This event can be sent to any element associated with a URL: 
images scripts, frames, iframes, and the window object.

您需要在正文加载时显示 div 并在加载时隐藏它。我不确定这是否是你要问的。如果它不是 setTimeout 函数,则可能是您的问题想。

From the jquery website

The load event is sent to an element when it and all sub-elements have been
completely loaded. This event can be sent to any element associated with a URL: 
images scripts, frames, iframes, and the window object.

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.

榕城若虚 2024-10-29 03:56:53

您的代码中有一个小问题:

window.setTimeout(function() {
$(document).ready(function() {
...

在此代码中,您为执行此操作的函数设置了超时:

function() {
    $(document).ready(function() {
        $('#overlay').remove();
    });
}

基本上它会等待一秒钟,然后向文档的就绪事件添加一个侦听器,并在该侦听器中执行实际的工作。因此,如果在第一秒内触发了就绪事件,该函数将根本不会执行。

您的代码应如下所示:

$(window).load(function() {
    setTimeout(function() {
        // Do the dom manipulations remove the placeholder
    }, 1000);
});

占位符将在加载事件后一秒内被删除。请注意,“加载”和“就绪”事件是不同的。并且“load”并不能保证 DOM 会被解析或渲染。

There's a small problem in your code:

window.setTimeout(function() {
$(document).ready(function() {
...

In this code you set the timeout, to the function that does this:

function() {
    $(document).ready(function() {
        $('#overlay').remove();
    });
}

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:

$(window).load(function() {
    setTimeout(function() {
        // Do the dom manipulations remove the placeholder
    }, 1000);
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文