繁体   English   中英

跨浏览器检查是否已加载图像

[英]Checking if an image is loaded, cross browser

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

我知道这件事:

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的方式做到这一点?

有点像:

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

应该管用。

正如它设置属性一样,图像确实已加载。 :)

我想你可以使用'onload'事件。

image.onload = function() {
};

但是,在设置图像的“ src”之前,应对此进行绑定。

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

<img src="whatever.png" onload="dostuff()" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM