2

I have an html page where in the middle there is a pic.
Is there a way to scale down the image with javascript in case it's too big for the current monitor resolution?

I mean if a user with a resolution of 1600*1200 open an image of 1900*1200 the image should be scaled down to fit the window.

I can use jquery

2 Answers2

5

Why not just use css?

.imgresize {
    max-width: 100%;
    max-height: 100%;        
}
Tetaxa
  • 4,375
  • 1
  • 19
  • 25
  • Hmm I think this solution got a problem. If someone with an iphone/mobile opens this page, the image will be always scaled down to the max-width of the iphone... hmm =/ – dynamic Oct 18 '11 at 18:09
  • have you tried it? it will scale to 100% of the parent element. if you don't want it to scale on a small screen try using css media queries http://www.google.com/search?q=css+media+queries – Tetaxa Oct 18 '11 at 18:23
1

Look here: Image resize to fit screen

else: http://jsfiddle.net/jsNDW/1/

HTML

<img src="http://placehold.it/1950x550" class="imgresize">

And JQUERY

var wWidth = $(window).width();
var wHeight = $(window).height();

$('.imgresize').each( function() {
var imgWidth = $(this).width();
var imgHeight = $(this).height();

    if(imgWidth >  wWidth)
    {
        $(this).width(wWidth);
    }

    if(imgHeight >  wHeight )
    {
        $(this).width(wHeight);
    }

});


$(window).resize(function() {
var wWidth = $(window).width();
var wHeight = $(window).height();

$('.imgresize').each( function() {
var imgWidth = $(this).width();
var imgHeight = $(this).height();

    if(imgWidth >  wWidth)
    {
        $(this).width(wWidth);
    }

    if(imgHeight >  wHeight )
    {
        $(this).height(wHeight);
    }

});

});
Community
  • 1
  • 1
Marco Johannesen
  • 13,084
  • 6
  • 31
  • 36