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

后端 未结 30 2346
傲寒
傲寒 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:46

    As Luke Smith says, image load is a mess. It's not reliable on all browsers. This fact has given me great pain. A cached image will not fire the event at all in some browsers, so those who said "image load is better than setTimeout" are wrong.

    Luke Smith's solution is here.

    And there is an interesting discussion about how this mess might be handled in jQuery 1.4.

    I have found that it's pretty reliable to set the width to 0, then wait for the "complete" property to go true and the width property to come in greater than zero. You should watch for errors, too.

提交回复
热议问题