获取图像的宽度和高度 [英] Get image width and height

查看:88
本文介绍了获取图像的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当没有为其指定样式(jQuery的css()返回0)时,我需要在javascript(jQuery)中获取图像的尺寸.

I need to get an image's dimensions in javascript (jQuery) when no styles are specified for it (jQuery's css() returns 0).

也许是因为我在询问尺寸之前正在使用jQuery加载图像.如果是这种情况,是否有任何事件可以监听,告诉您何时加载了图像?

Maybe it is because I'm loading the image with jQuery just before asking for its dimensions. If this is the case, is there any event to listen that tells when the image has been loaded?

推荐答案

也许图像未完全加载,因此无法给出尺寸.但是如果没有您的代码,我无法告诉您您在做什么错,但是在这里是一个可以正常工作的示例:

Maybe the image has not fully loaded therefore the dimensions can not be given.But without your code I can't tell what you're doing wrong, but here is an Example that would work:

function LoadImage(isrc) {
    var oImg = new Image();
    oImg.src = isrc;
    if (oImg.complete) {
        window.alert(oImg.src + ' ' + oImg.width + ' x ' + oImg.height);
    }
    else {
        window.setTimeout('iLoad(imgsrc)', 1000);
    }
}

<body onLoad='LoadImage(imgsrc)'>

这篇关于获取图像的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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