我已经使用过类似的方法来预加载图像,然后在图像加载完成后自动调用我的Javascript。您想在设置回调之前检查完成情况,因为该图像可能已经被缓存,并且可能不会调用您的回调。
function PreloadImage(imgSrc, callback){ var objImagePreloader = new Image(); objImagePreloader.src = imgSrc; if(objImagePreloader.complete){ callback(); objImagePreloader.onload=function(){}; } else{ objImagePreloader.onload = function() { callback(); // clear onLoad, IE behaves irratically with animated gifs otherwise objImagePreloader.onload=function(){}; } }}


