How to get image size (height & width) using JavaScript?

前端 未结 29 3002
忘了有多久
忘了有多久 2020-11-21 05:23

Are there any JavaScript or jQuery APIs or methods to get the dimensions of an image on the page?

29条回答
  •  天涯浪人
    2020-11-21 05:58

    You can programmatically get the image and check the dimensions using Javascript...

    const img = new Image();
    img.onload = function() {
      alert(this.width + 'x' + this.height);
    }
    img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

    This can be useful if the image is not a part of the markup.

提交回复
热议问题