简体   繁体   English

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

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

I need a cross browser solution to find if an image is done loading. 我需要一个跨浏览器解决方案来查找图像是否已完成加载。

I know about this: 我知道这件事:

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

Is that cross browser compatible (by that I mean FF, Chrome, Safari, Opera, IE8+)? 这是跨浏览器兼容(我的意思是FF,Chrome,Safari,Opera,IE8 +)? If not, how can I do this in a cross browser and jQuery-less way? 如果没有,我怎么能在跨浏览器和jQuery的方式做到这一点?

Something as: 有点像:

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

Should work. 应该管用。

Just as it set attributes as the image is really loaded. 正如它设置属性一样,图像确实已加载。 :) :)

I think you can use the 'onload' event. 我想你可以使用'onload'事件。

image.onload = function() {
};

This should be bound though, before you set the 'src' of the image. 但是,在设置图像的“ src”之前,应对此进行绑定。

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

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

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

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