3

I am making an image gallery. This is the HTML for the image:

<div style="width: 84%; height: 100%; float: left; text-align: center;"><img
id="mainimage" style="height: 100%;" 
src="http://www.gymheroapp.com/workouts/img/spinner.gif"/></div>
<!-- Loading spinner is temp image, will be replaced by Javascript later -->

It works fine when the image is square, or the width is not too much more than the height. It breaks when the width is too great, though. Example of image overflowing:

image overflowing

Is there a way I could check whether there is overflow within my Javascript? Something like this:

image.setAttribute('height', '100%')
image.removeAttribute('width')
if (image.isOverflowing()) {
    image.setAttribute('width', '100%')
    image.removeAttribute('height')
}

Even better, is there any way to scale the image properly but fit it withing the containing div?

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Possible duplicate http://stackoverflow.com/questions/2059743/detect-elements-overflow-using-jquery – JohnJohnGa Jan 20 '13 at 14:16
  • 1
    @JohnJohnGa That's a completely different question. I don't want scrollbars. I want to resize my image. Also I am not using JQuery. – tckmn Jan 20 '13 at 14:17
  • Ah, I gave an answer that resizes them, but in jquery. I've deleted it. – Popnoodles Jan 20 '13 at 14:23
  • @popnoodles No wait! That was helpful. I can change it to vanilla JS. Undelete it and I will accept it :) – tckmn Jan 20 '13 at 14:25
  • oh ok. I've forgotten how to traverse up to parent using vanilla. If you know what you're doing no problem. – Popnoodles Jan 20 '13 at 14:26
  • @popnoodles I have access to the parent element and I know it won't change so it's good. :) Also it's just `element.parentNode` – tckmn Jan 20 '13 at 14:27

2 Answers2

16

jquery example

Best fit:

$('img').on('load',function(){
    var css;
    var ratio=$(this).width() / $(this).height();
    var pratio=$(this).parent().width() / $(this).parent().height();
    if (ratio<pratio) css={width:'auto', height:'100%'};
    else css={width:'100%', height:'auto'};
    $(this).css(css);
});

Edit to account for caveat where image is in cache

$('img').on('bestfit',function(){
    var css;
    var ratio=$(this).width() / $(this).height();
    var pratio=$(this).parent().width() / $(this).parent().height();
    if (ratio<pratio) css={width:'auto', height:'100%'};
    else css={width:'100%', height:'auto'};
    $(this).css(css);
}).on('load', function(){
    $(this).trigger('bestfit');
}).trigger('bestfit');
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
7

Heres a simple CSS solution:

html, body { height: 100%; }
#gallery { height: 100%; width: 100%; position: relative; }

#gallery img {
  /* CSS Hack will make it width 100% and height 100% */
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  /* Maintain aspect ratio */
  max-height: 100%;
  max-width: 100%;
}
<div id="gallery">
  <img src="http://cl.vvmonnickendam.nl/files/large/87" alt="Awesome blabla gedoe">
</div>

Fiddle example

Chris Visser
  • 1,607
  • 12
  • 24