我需要能够等到图像的高度和宽度可用时才启动函数。当此代码调用 start()
时,它仍然表示高度和宽度为零:
var img = new Image();
init = function() {
img.onload = this.start();
img.src = "sky.jpg";
}
start = function() {
alert("Start called.\nwidth:"+img.width+"\nheight"+img.height);
}
init();
如何让它等到维度可用后再调用 start()
?
最佳答案
var img = new Image();
var start = function() {
alert("Start called.\nwidth:"+img.width+"\nheight"+img.height);
}
var init = function() {
img.onload = start;
img.src = "sky.jpg";
}
init();
将 this.start()
更改为 start
。
我还确定了您的功能范围。
关于javascript - 如何在 Javascript 中收到图像尺寸可用的提醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4341796/