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

后端 未结 30 2358
傲寒
傲寒 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条回答
  •  天涯浪人
    2020-11-22 01:40

    To add to Xavi's answer, Paul Irish's github David Desandro's gitgub offers a function called imagesLoaded() that works on the same principles, and gets around the problem of some browser's cached images not firing the .load() event (with clever original_src -> data_uri -> original_src switching).

    It's is widely used and updated regularly, which contributes to it being the most robust solution to the problem, IMO.

提交回复
热议问题