How do I get actual image width and height using jQuery?

前端 未结 5 1039
情书的邮戳
情书的邮戳 2020-11-30 05:49

On a page I have displayed 100 images, which have the width and height attribute changed. Image id is in a loop.

How do I get the original image size, inside this

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 06:44

    You can retrieve the original dimensions of an image by using the naturalWidth and naturalHeight properties of the dom object.

    Eg.

    var image = document.getElementById('someImage');
    
    var originalWidth = image.naturalWidth; 
    var originalHeight = image.naturalHeight;
    

    With jQuery it would look like:

    var image = $('#someImage');
    
    var originalWidth = image[0].naturalWidth; 
    var originalHeight = image[0].naturalHeight;
    

提交回复
热议问题