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

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

    How we get right real dimensions without a blink real image:

    (function( $ ){
       $.fn.getDimensions=function(){
             alert("First example:This works only for HTML code without CSS width/height definition.");
             w=$(this, 'img')[0].width;
             h=$(this, 'img')[0].height;
    
             alert("This is a width/height on your monitor: " + $(this, 'img')[0].width+"/"+$(this, 'img')[0].height);
    
             //This is bad practice - it shows on your monitor
             $(this, 'img')[0].removeAttribute( "width" );
             $(this, 'img')[0].removeAttribute( "height" );
             alert("This is a bad effect of view after attributes removing, but we get right dimensions: "+  $(this, 'img')[0].width+"/"+$(this, 'img')[0].height);
             //I'am going to repare it
             $(this, 'img')[0].width=w;
             $(this, 'img')[0].height=h;
             //This is a good practice - it doesn't show on your monitor
             ku=$(this, 'img').clone(); //We will work with a clone
             ku.attr( "id","mnbv1lk87jhy0utrd" );//Markup clone for a final removing
             ku[0].removeAttribute( "width" );
             ku[0].removeAttribute( "height" );
             //Now we still get 0
             alert("There are still 0 before a clone appending to document: "+ $(ku)[0].width+"/"+$(ku)[0].height);
             //Hide a clone
             ku.css({"visibility" : "hidden",'position':'absolute','left':'-9999px'}); 
             //A clone appending
             $(document.body).append (ku[0]);
             alert("We get right dimensions: "+ $(ku)[0].width+"/"+$(ku)[0].height);
             //Remove a clone
             $("#mnbv1lk87jhy0utrd").remove();
    
             //But a next resolution is the best of all. It works in case of CSS definition of dimensions as well.
             alert("But if you want to read real dimensions for image with CSS class definition outside of img element, you can't do it with a clone of image. Clone method is working with CSS dimensions, a clone has dimensions as well as in CSS class. That's why you have to work with a new img element.");
             imgcopy=$('');//new object 
             imgcopy.attr( "id","mnbv1lk87jhy0aaa" );//Markup for a final removing
             imgcopy.css({"visibility" : "hidden",'position':'absolute','left':'-9999px'});//hide copy 
             $(document.body).append (imgcopy);//append to document 
             alert("We get right dimensions: "+ imgcopy.width()+"/"+imgcopy.height());
             $("#mnbv1lk87jhy0aaa").remove();
    
    
       }
    })( jQuery );
    
    $(document).ready(function(){
    
       $("img.toreaddimensions").click(function(){$(this).getDimensions();});
    });
    

    It works with

提交回复
热议问题