如何获得高度和宽度图像javascript

时间:2014-07-17 21:23:30

标签: javascript jquery html css

        function readImage(inputFiles,$input){
            if(inputFiles == undefined || inputFiles.length == 0) return;

            var inputFile = inputFiles[0];

            var reader = new FileReader();

            reader.readAsDataURL(inputFile);

            reader.onload = function(event) {

                $input.attr("src", event.target.result);

            var image = new Image();
            image.src = event.target.result;
            image.onload = function() {
                console.log('height: ' + this.height); ** <=== this * **
                console.log('width: ' + this.width);   ** <=== and this * **
            };

            };
        }

我如何使它们成为全局var

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

我希望当我加载图像时,我可以从另一个函数获得高度和宽度 喜欢:

function getSize(){
  return this.height+'|'+this.width;
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码行获取图像大小:

var image = document.getElementById ('image');
console.log (image.clientWidth + "x" + image.clientHeight)

Sample code in fiddle.