JQuery预加载页面

时间:2011-11-24 21:02:21

标签: php jquery load

我的问题很简单,我想在显示div之前预先加载页面

我的索引页面中有一个主div:#data_div

我将包含大量图像的页面加载到该Div ...(不仅是图像,还有很多标记)

我使用另一个div来预加载页面,div不可见(但不能隐藏以保持加载数据的良好格式化):#data_loading;

我使用div来加载符号:#wait_div

我认为我需要等同于JQUERY的ajax加载(事件,不是加载方法) 但我无法将此事件设置为div(...)所以我需要另一种解决方案

        $.address.change(function(event) {
                    $("#wait_div").show();

                    if(event.value == "/") {
                        $.address.value("index.php");
                    } else if(event.value.substr(1, 5) == "Datas") {
                        $("#data_loading").load(event.value, {
                            'ajax' : 'true'
                        },function(){

    /* i would like that this occur only when the page is fully loaded 
(including image and scripts) inside the temp div */
                           $("#data_div").children().remove();
                           $("#wait_div").hide();
                           $("#data_loading").children().appendTo($("#data_div")).fadeIn(1000);
                                //alert("done");
                            });
                        });
                    }
                });

2 个答案:

答案 0 :(得分:1)

检查this回答我的问题

Thant是你知道加载所有图像的方法,你可以给你的临时图像中的图像一个类,并在你的新页面中做这样的事情:

  $(document).ready(function(){
    var imgs = $("img.tempDivImages"), cnt = imgs.length;

    imgs
    .load(function(){
      if(!--cnt) {
        /* all images loaded */
        /* now call the function you want when everything is loaded*/
      }
    })
    .error(function() { /* whoops an image failed to load */});
  });

当你加载临时div中的所有内容时,这就是你如何使用calla函数或做某事。

答案 1 :(得分:0)

我在another question(不是jQuery而是JavaScript)中回答了这个问题:

注意:您可以在HTML中添加加载消息,并在preload()函数结束时隐藏它。

<强>的JavaScript

function preload() {
    imageObj = new Image();
    images = new Array();
    images[0] = 'img/1.png';
    images[1] = 'img/2.png';
    images[2] = 'img/3.png';
    images[3] = 'img/4.png';
    images[4] = 'img/5.png';

    for (i = 0; i <= 4; i++)
        imageObj.src = images[i];
}

<强> HTML

<body onload="preload();">
    ....

    <!--[if IE]>
        <div id="preload">
            <img src="img/1.png" width="1" height="1" alt="" />
            <img src="img/2.png" width="1" height="1" alt="" />
            <img src="img/3.png" width="1" height="1" alt="" />
            <img src="img/4.png" width="1" height="1" alt="" />
            <img src="img/5.png" width="1" height="1" alt="" />
        </div>
    <![endif]-->
</body>

IE for IE

#preload {
   position: absolute;
   overflow: hidden;
   left: -9999px; 
   top: -9999px;
   height: 1px;
   width: 1px;
}
相关问题