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

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

    $(document).ready(function(){
                                var image = $("#fix_img");
                                var w = image.width();
                                var h = image.height();
                                var mr = 274/200;
                                var ir = w/h
                                if(ir > mr){
                                    image.height(200);
                                    image.width(200*ir);
                                } else{
                                    image.width(274);
                                    image.height(274/ir);
                                }
                            }); 
    

    // This code helps to show image with 200*274 dimention

提交回复
热议问题