Get the real width and height of an image with JavaScript? (in Safari/Chrome)

后端 未结 30 2408
傲寒
傲寒 2020-11-22 01:16

I am creating a jQuery plugin.

How do I get the real image width and height with Javascript in Safari?

The following works with Firefox 3, IE7 and Opera 9:

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 01:42

    Use the naturalHeight and naturalWidth attributes from HTML5.

    For example:

    var h = document.querySelector('img').naturalHeight;
    

    Works in IE9+, Chrome, Firefox, Safari and Opera (stats).

提交回复
热议问题