开发者

get image size using javascript or jquery

I want to get the image size using javascript or jquery. something similar开发者_JS百科 to imagesize().

Thanks Jean


Has already been asked and answered - How to get image size (height & width) using JavaScript?

var img = document.getElementById('imageid'); 
var width = img.clientWidth;
var height = img.clientHeight;


That depends. If the image already has width/height styles/attributes applied to it, you will not get the image's width/height, but you will get the resized dimensions. Here's a possible solution:

function getImageSize(img) {
    var clone = img.clone();
    clone.width("auto")
         .height("auto")
         .css("position", "absolute")
         .css("left", -9999)
         .css("top", -9999);
    $(document.body).append(clone);
    var width = clone.width();
    var height = clone.height();
    clone.remove();
    return [width, height];
}

Untested, but it should work. However, if you're sure the image is not resized from css/html, there's no need for this complication and you can use what the others have said.


var width = $("#img").width();


you can use .attr('width') and .attr('height') unless you mean size in bytes?

this wouldnt work if the img is being rescaled with css

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜