获取图像宽度高度的JavaScript方法

3

嗯,我有这段代码:

http://jsfiddle.net/hv9gB/3/

    (function() {

    var img = document.getElementById('my_image');

    // Original
    var width, height;

    // Display
    var d_width = img.width;
    var d_height = img.height;

    var updateDetail = function() {
        document
            .getElementById('display_size')
            .innerHTML = 'Display Size: ' + d_width + ' x ' + d_height;

        document
            .getElementById('native_size')
            .innerHTML = 'Original Size: ' + width + ' x ' + height;
    };

    // Using naturalWidth/Height
    if (img.naturalWidth) {
        width = img.naturalWidth;
        height = img.naturalHeight;

        updateDetail();
    } else {
        // Using an Image Object
        img = new Image();
        img.onload = function() {
            width = this.width;
            height = this.height;

            updateDetail();
        };
        img.src = 'http://lorempixel.com/output/nature-q-c-640-480-3.jpg';
    }

}());

那就是我的网站:

http://dhems.x10.mx/

但是在我的网站上,图片的宽度和高度无法显示。为什么?!?!
4个回答

5
将你的代码放在 中。
window.onload = function() {
 //insert code here
}

您的JS文件在引擎到达图片之前已被执行。您将收到以下控制台错误:

Uncaught TypeError: Cannot read property 'width' of null 

将所有脚本文件放在页面末尾,将所有CSS文件放在开头也是一个好习惯。这将增加页面的加载时间,因为如果JS文件在开头,只有当这些文件被下载后,您的浏览器才会开始呈现。

你的代码应该像这样:

window.onload = function() {

var img = document.getElementById('my_image');

// Original
var width, height;

// Display
var d_width = img.width;
var d_height = img.height;

var updateDetail = function() {
    document
        .getElementById('display_size')
        .innerHTML = 'Display Size: ' + d_width + ' x ' + d_height;

    document
        .getElementById('native_size')
        .innerHTML = 'Original Size: ' + width + ' x ' + height;
};

// Using naturalWidth/Height
if (img.naturalWidth) {
    width = img.naturalWidth;
    height = img.naturalHeight;

    updateDetail();
} else {
    // Using an Image Object
    img = new Image();
    img.onload = function() {
        width = this.width;
        height = this.height;

        updateDetail();
    };
    img.src = 'http://lorempixel.com/output/nature-q-c-640-480-3.jpg';
}

}

是的,只需将您的代码放在 window.onload = function() { code here } 中。 - Deepsy
不用谢。顺便考虑将最佳答案标记为已回答,这样其他人就不会无缘无故地进来了。 - Deepsy

0
将代码放置在您的底部,使用

谢谢你的回答!但我想要在.js文件中的代码,这样我才能学习:D - user2491036

0
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
function readImage(file) {

    var reader = new FileReader();
    var image  = new Image();

    reader.readAsDataURL(file);  
    reader.onload = function(_file) {
        image.src    = _file.target.result;              // url.createObjectURL(file);
        image.onload = function() {
            var w = this.width,
                h = this.height,
                t = file.type,                           // ext only: // file.type.split('/')[1],
                n = file.name,
                s = ~~(file.size/1024) +'KB';
            $('#uploadPreview').append('<img src="'+ this.src +'"> '+w+'x'+h+' '+s+' '+t+' '+n+'<br>');
        };
        image.onerror= function() {
            alert('Invalid file type: '+ file.type);
        };      
    };

}
$("#choose").change(function (e) {
    if(this.disabled) return alert('File upload not supported!');
    var F = this.files;
    if(F && F[0]) for(var i=0; i<F.length; i++) readImage( F[i] );
});
</script>
<meta charset=utf-8 />
<title>Multiple image upload with preview by Roko C.B.</title>
</head>
<body>
  <input type="file" id="choose" multiple="multiple" />
<br>
<div id="uploadPreview"></div>
</body>
</html>

0
将HTML中的脚本标签移动到图像后面。在图像进入页面之前执行脚本。

你看过这个脚本吗?window.onload = function() { - j08691
变量img = document.getElementById('my_image');不在onload中。 - Jan Tojnar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接