Javascript:获取多个图像数组的图像大小(宽度和高度)

时间:2013-10-09 10:44:18

标签: javascript arrays image asynchronous

如何调整此解决方案(来自此处的代码:https://stackoverflow.com/a/626505/975443

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

用于多个图像(用于循环遍历数组)?

2 个答案:

答案 0 :(得分:2)

解决方案:

var asyncI = 0;
var asyncCount = 10;

function readImageDimensions() {

    console.log('asyncI: ' + asyncI);

    var img = new Image();
    img.src = 'data/images/' + asyncI + ".png";

    img.onload = function() {

        console.log('this.width: ' + this.width + ', this.height: ' + this.height);

        asyncI++;

        if (asyncI < asyncCount) {

            //go to next loop iteration
            console.log('continue ' + asyncI);
            readImageDimensions();

        } else {

            //exit loop
            console.log('END');
            functionToContinueWith();

        }
    }
}

//call the function
readImageDimensions();

答案 1 :(得分:0)

这可能会对你有所帮助

      for (var i=0;i<number_of_photos;i++){ 
widthheight[i]=width + 'x' + height
}

通过alert(widthheight[0]);

提醒第一个

因为我不熟悉你的代码和我不知道你真正想要什么,不能帮助更多

你正在使用jquery吗?