2

我需要一个跨浏览器解决方案来查找图像是否已完成加载。

我知道这件事:

var theImage = new Image()
theImage.src = 'http://pieisgood.org/images/slice.jpg' //yum, pie :)
//later...
if (theImage.complete) {
    //do stuff
}

是否兼容跨浏览器(我的意思是 FF、Chrome、Safari、Opera、IE8+)?如果没有,我该如何以跨浏览器和无 jQuery 的方式执行此操作?

4

3 回答 3

4

比如:

var theImage = new Image()
theImage.src = 'http://pieisgood.org/images/slice.jpg' //yum, pie :)
theImage.isLoaded = false;
//later...
theImage.onload = function(){
    theImage.isLoaded = true;
    //do stuff
}
if(theImage.isLoaded){
    alert("Loaded");
}
document.body.appendChild(theImage); // VERY needed

应该管用。

就像它在真正加载图像时设置属性一样。:)

于 2013-04-26T23:44:58.963 回答
2

我认为您可以使用“onload”事件。

image.onload = function() {
};

但是,在设置图像的“src”之前,这应该是绑定的。

于 2013-04-26T23:44:56.667 回答
1

或者只是通过 html 控制它并在指定的 img onload 事件上调用你的函数

<img src="whatever.png" onload="dostuff()" />
于 2013-04-26T23:47:24.807 回答