【问题标题】:Javascript: get image size (width & height) for an array of multiple imagesJavascript:获取多个图像数组的图像大小(宽度和高度)
【发布时间】:2013-10-16 16:22:41
【问题描述】:

如何调整这个解决方案(代码来自这里: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';

对于多张图片(for循环遍历一个数组)?

【问题讨论】:

    标签: javascript arrays image asynchronous


    【解决方案1】:

    解决方案:

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

    【讨论】:

      【解决方案2】:

      这可能对你有帮助

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

      通过alert(widthheight[0]);提醒第一个

      因为我不熟悉您的代码并且我不知道您真正想要什么,所以无法提供更多帮助

      你在用 jquery 吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-06
        • 2013-06-19
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多