2012-12-08 267 views
0

我的目標是在加載實際圖像時進行圖像預加載,但我無法弄清楚。整個目標是我有一個幻燈片顯示圖像,但有時它加載緩慢..大聲笑。這裏是我的HTML:圖片預加載javascript

<div id="slideshow"> 
    <img src="../images/slideshow/spacer.gif" width="100" height="100" id="myPicture" alt="some image"> 
</div> 

這裏是我的javascript我已經:

window.onload = choosePic; 

function choosePic() { 
    var myPix = new Array("../images/slideshow/sempiternal.png", "../images/slideshow/cross.png", "../images/slideshow/pentagram.png"); 
    var randomNum = Math.floor((Math.random() * myPix.length)); 
    document.getElementById("myPicture").src = myPix[randomNum]; 
} 

而CSS:

#slideshow { 
    height: 100px; 
    width: 100px; 
    position: fixed; 
    bottom: 20px; 
    right: 20px; 
} 

回答

0

似乎並不像你的函數將預裝任何東西。除了正常加載的隨機圖像之外,陣列中的其他圖像根本不會被調用。

您可能想要查看JavaScript Preloading ImagesFunction to preload images?

+0

這就是我的想法,但我想看看是否有其他人可以想到的東西。謝謝你的時間! –