0

我的目标是在加载实际图像时预加载图像,但我不太明白。这样做的整个目标是我有一个显示图像的幻灯片,但有时加载速度很慢......哈哈。这是我的 HTML:

<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;
}
4

1 回答 1

0

似乎您的函数不会预加载任何内容。除了选择正常加载的随机图像外,数组中的其他图像根本不会被调用。

您可能想查看JavaScript Preloading ImagesFunction to preload images?反而。

于 2012-12-08T03:34:12.223 回答