Can Javascript access native image size?

前端 未结 5 1161
暖寄归人
暖寄归人 2021-01-03 20:30

I\'m writing a jQuery function where I\'d like to access both the native size of an image, and the size specified for it on the page. I\'d like to set a variable for each.

5条回答
  •  有刺的猬
    2021-01-03 20:47

    This should work:

    var img = $('#imageid')[0]; //same as document.getElementById('imageid');
    var width = img.naturalWidth;
    var height = img.naturalHeight;
    

    The naturalWidth and naturalHeight return the size of the image response, not the display size.

    According to Josh' comment this is not supported cross browser, this might be correct, I tested this in FF3

提交回复
热议问题