2010-12-03 214 views
0

我想創建一個完整頁面的預加載器。預加載頁面

<body> 
<div id="preload-content">Please wait</div> 
<div id="body-container" style="background-image:url(very-big-image-file.jpg);"> 
// page contents goes here 
</div> 
</body> 

現在我在我的#body-container中有一個巨大的圖像。 我使用jQuery,當我使用.ready()函數頁面出現,但圖像仍在加載。

$("body").ready(function() { 
    $("#preload-content").delay(500).fadeOut(400, function() { 
     $("#body-container").fadeIn(200); 
     $("body").css("background-color","#fff").css("color","#000"); 
    }); 
}); 

$(document).ready()也不起作用。 我想顯示所有圖像,插件,腳本,css文件加載成功後的#body-container! 所以......我該怎麼做。

+1

圖像將在單獨的線程加載。所以,它不會影響ready()函數。 – WaiLam 2010-12-03 08:57:11

回答

1

您可以試試。

 $("body").ready(function() { 

     // Create a image object 
     var bgImage = new Image(); 

     // bind loading event 
     $(bgImage).bind("onload",function(){ 

      // Set the background css 
      $("#body-container").css('background-image','url(very-big-image-file.jpg)'); 

      // Fade your preloader content 
      $("#preload-content").delay(500).fadeOut(400, function() { 
       $("#body-container").fadeIn(200); 
       $("body").css("background-color","#fff").css("color","#000"); 
      }); 
     }); 

     //Start loading your image 
     bgImage.src = "very-big-image-file.jpg"; 

     }); 

爲某些錯字錯誤道歉。但是這個概念就是這樣。

+0

但我能做些什麼,如果我的網頁上有更大的圖像?我必須將所有圖像onload函數綁定到對方嗎? – mrtakdnz 2010-12-03 09:15:19

+0

是的,你可以創建一個預加載函數來輸入一個你想要預加載的imge路徑的數組。但我不認爲在頁面顯示之前預先加載大量的圖像並不好。 – WaiLam 2010-12-03 09:24:04

1

當你調用$("anything").ready()時,它全部到達相同的地方(當DOM準備就緒時,事件處理程序,不一定是所有資源),選擇器實際上並不重要。

你在這裏想要什麼,雖然是window.onload,當圖像已加載爲好,這樣它將觸發:

$(window).load(function() { 
    $("#preload-content").delay(500).fadeOut(400, function() { 
     $("#body-container").fadeIn(200); 
     $("body").css("background-color","#fff").css("color","#000"); 
    }); 
}); 

確認這是之外的任何document.ready處理程序,因爲有一個機會onload事件已經執行。

0

嘗試這一個:

<html> 
    <head> 
     <!-- Include the heartcode canvasloader js file --> 
    <script src="http://heartcode-canvasloader.googlecode.com/files/heartcode-canvasloader-min-0.9.1.js"></script> 

     <style type="text/css"> 
     body, html { 
      margin:0; 
      padding:0; 
      overflow:hidden; 
      background-color:#353535; 
     } 
     .wrapper { 
      position:absolute; 
      top:50%; 
      left:50%; 
     } 
      #contenedor { 
        background-color: #fff; 
      } 
    </style> 
    </head> 

    <body onLoad="document.getElementById('precarga').style.visibility='hidden';document.getElementById('contenedor').style.visibility='visible';" > 

     <div id="precarga" style="visibility: visible"> 
      <!-- Create a div which will be the canvasloader wrapper -->  
    <div id="canvasloader-container" class="wrapper"></div> 

    <!-- This script creates a new CanvasLoader instance and places it in the wrapper div --> 
    <script type="text/javascript"> 
     var cl = new CanvasLoader('canvasloader-container'); 
     cl.setColor('#9e9e9e'); // default is '#000000' 
     cl.setShape('spiral'); // default is 'oval' 
     cl.setDiameter(200); // default is 40 
     cl.setDensity(160); // default is 40 
     cl.setRange(1); // default is 1.3 
       cl.setSpeed(1); // default is 2 
     cl.setFPS(60); // default is 24 
     cl.show(); // Hidden by default 

     // This bit is only for positioning - not necessary 
     var loaderObj = document.getElementById("canvasLoader"); 
     loaderObj.style.position = "absolute"; 
     loaderObj.style["top"] = cl.getDiameter() * -0.5 + "px"; 
     loaderObj.style["left"] = cl.getDiameter() * -0.5 + "px"; 
    </script> 
     </div> 

     <div id="contenedor" style="visibility: hidden"> 
      <!-- Modify this with your content [start] --> 
      <p align="justify"> 
       Lorem ipsum dolor sit amet, sit rhoncus tellus tristique ipsum, etiam enim commodo purus ligula, dui praesent vivamus fusce vel, justo vitae nesciunt suspendisse. Pretium elit maecenas mollis pellentesque, leo in vestibulum vitae habitasse dolor lacus, elit pharetra nec cras suspendisse, lectus eget nulla elit. Ut sapien velit quis accumsan nibh varius. Nunc maecenas, eu sapien porttitor convallis, nullam cras. Dolores mauris cum at velit, mauris posuere nunc euismod. Vel leo volutpat scelerisque aliquam mi.</p> 
      <p align="justify"> 
       Aenean sed per nec in facilisis ullamcorper, mattis cras neque mauris fusce. Non odio gravida et, aliquet laoreet ac consequat risus, ut ridiculus sit massa est. A tristique adipiscing nulla, vel interdum magna. Luctus tincidunt curabitur erat ac, nisl praesent odio, in ullamcorper sem, proin tortor, dignissim feugiat orci non quam vitae mi. Id amet, vitae magna nunc velit vel, cras wisi tincidunt magnam at donec venenatis. Rutrum quis turpis eget. Nec quis donec sed non ultrices gravida, suscipit varius vivamus, velit libero quis imperdiet molestie.</p> 
      <p align="justify"> 
       Pellentesque ornare eu lacus non, pede tempus velit duis, gravida nam phasellus sem, amet accumsan tincidunt wisi. Et libero et neque, aliquam pretium rerum neque, feugiat malesuada est sed autem a platea, lectus ultrices. Vitae tristique aliquet, tincidunt wisi velit odio sodales nunc nunc. Sed tincidunt turpis a nullam magna, sagittis vestibulum pellentesque integer montes neque, nunc odio neque diam lorem integer, sociis eu. Maecenas ut lectus senectus auctor dictum, ligula volutpat nulla pulvinar nam pellentesque pellentesque, vitae morbi magna dui in risus, aliquam vel mi odio purus duis suspendisse, justo mattis morbi integer. Pretium nec a, duis nunc mi, in vel. Lacus dui, eros nulla justo sollicitudin. Turpis et et pharetra non.</p> 
      <p align="justify"> 
       Nunc maecenas turpis, vel in sed eget arcu, elit vitae. Curabitur et odio in ornare metus est. Massa porttitor consectetuer. Vitae bibendum, consectetuer duis diam quam in, est etiam aliquam enim molestie scelerisque. Vestibulum sed eros ac vitae lectus. A leo, quod volutpat id et lacus vitae vitae. Aenean tellus vivamus parturient metus, pellentesque ut lorem ut orci nisl. Ut et id luctus eros, feugiat in malesuada tellus quisque, iaculis tristique, nonummy massa voluptas vel. Elit a erat pulvinar sem sagittis, ligula eget est amet, a tristique ut ac wisi, gravida donec et eget nec. Amet nec, ut non penatibus eget, ornare purus sollicitudin a tincidunt vitae rutrum, commodo non faucibus, aut pretium risus lorem nulla egestas auctor.</p> 
      <p align="justify"> 
      </p> 
      <img src="http://v11.nonxt8.c.bigcache.googleapis.com/static.panoramio.com/photos/original/21785753.jpg?redirect_counter=1"/> 
      <!-- Modify this with your content [end] --> 
     </div> 

    </body> 
</html>