如何在javascript var中预加载html

时间:2009-07-02 07:59:54

标签: javascript jquery preloader

我有一个我正在研究的网站。我正在使用jquery来动画和显示内容。此内容存储在变量中。我需要知道在显示内容之前如何加载内容。

为了澄清,您单击一个链接,“加载...”窗口淡入,一旦加载所有内容,它就会淡出并淡化存储在变量中的已加载内容。

谢谢

2 个答案:

答案 0 :(得分:0)

您是否正在寻找如何通过AJAX请求HTML内容,知道何时完成,然后将其插入DOM?如果是这样,jQuery的load方法可能就是您所追求的。

史蒂夫

答案 1 :(得分:0)

AJAX事件不会告诉你加载了多少百分比,事实上,在大多数情况下,它不知道响应有多长。但它会在响应完成或发生错误时通知您。

查看官方参考AJAX of JQuery。我原来的答案是错的,因为我想你已经有了数据。 ajax请求的简化用例是:

> Initiate the Request, and set the handler for ajax complete (thru something like $.Ajax)
> Hide the content pane and show the loader
> When ajax complete, you display your content, and hide the loader

以下是原始答案。


我认为您正在谈论客户端计算机内存中已有的内容,但您希望在完成加载后立即显示所有内容。听起来像离线媒体中的“双缓冲”。

你可以做的是:

// Display the loading screen, you can put any animation
$("#loader").fadeIn();
$("#contentPlaceHolder").hide();
// attach the DOM of the contents to placeholder.
$("#contentPlaceHolder").append(CONTENTS);
// .... similar statements follows.
// and finally..
$("#contentPlaceHolder").show();
$("#loader").fadeOut();