【发布时间】:2012-07-06 22:47:16
【问题描述】:
一直在寻找预加载图像目录的方法,发现这是此处提供的最佳脚本:http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/ 我最喜欢这个,因为它在预加载之前等待当前文档加载,但我想让这个人加载一个图像目录而不是指定每个文件,而且我对 javascript 很陌生,任何帮助表示赞赏。
function preloader() {
if (document.images) {
var img1 = new Image();
var img2 = new Image();
var img3 = new Image();
img1.src = "http://domain.tld/path/to/image-001.gif";
img2.src = "http://domain.tld/path/to/image-002.gif";
img3.src = "http://domain.tld/path/to/image-003.gif";
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(preloader);
【问题讨论】:
标签: image caching load onload preload