使用Javascript:获得的图像尺寸(宽&安培;高度)为多个图像阵列 [英] Javascript: get image size (width & height) for an array of multiple images

查看:128
本文介绍了使用Javascript:获得的图像尺寸(宽&安培;高度)为多个图像阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从这里适应这个解决方案(code: http://stackoverflow.com/a/626505/975443

How to adapt this solution (code from here: http://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';

多个图像(遍历数组)?

for multiple images (for loop through an array)?

推荐答案

解决方案:

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();

这篇关于使用Javascript:获得的图像尺寸(宽&安培;高度)为多个图像阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆