10

I have a div call it #container, Inside this #container I have n amount of img tags call it images n can be 2, 10, 40 and so on. I am wondering how I can fit n amount of images inside a #container to close all white spaces stretch the images. Quality doesn't matter This is what I tried until now:

var amount = $("#container > img").length;
var amount_w = amount*200; //200 width of 1 image 
var amount_h = amount*130; //120 height  image 
var refH = $("#container").height();
var refW = $("#container").width();

var refRatio = refW/refH;
$("#container img").each(function(){
     $(this).height((amount_h-130)-refH);
     $(this).width((amount_w-230)-refW);
});
SergkeiM
  • 3,934
  • 6
  • 36
  • 69

3 Answers3

3

First of all, it IS possible to achieve what you need even while maintaining the aspect ratio of the images - however the row height will be calculated, but it is not a trivial task (well, at least not as trivial as a single line formula).

There is a jQuery plugin called jPictura which I have developed. I believe the plugin does exactly what you need.

Here is a working fiddle.

You can find the plugin source codes and documentation on GitHub.

Simple example how to use the plugin:

$(document).ready(function () {
    $('#my-gallery').jpictura({
        layout: { itemSpacing: 0, justifyLastRow: true, idealRowHeight: 200}
    });
});
  • itemSpacing - amount of space between the images in pixels
  • justifyLastRow - if true, the images in last row will be stretched to take the full width of the row
  • idealRowHeight - The desired height of the rows in pixels. The plugin will do its best to arrange the items so the row heights are as close as possible to this value.
  • there are a lot more options documented on GitHub

Beside the JS stuff that calculates the correct widths and heights of the images, there is one more thing to be considered regarding the blank space between images. Images are by default inline-blocks which means they behave like words and words do have some white space inbetween, right? Make them display: block; float: left; or use the flex box layout to get rid of the blank space. The plugin uses float: left; by default.

Anton
  • 2,458
  • 2
  • 18
  • 30
  • Thank you but before to apply this I need to resize them based on container width and height. So they fit container without white space left! – SergkeiM Jan 28 '16 at 07:32
  • 1
    @Froxz, Yeah, I got the point now - it is not a trivial task if you want to make it properly - see [this link](http://www.domjudov.sk/sk/fotogaleria/album/?idAlbum=86&back=http%3A%2F%2Fwww.domjudov.sk%2Fsk%2Ffotogaleria%2F). I made a jQuery plugin for a gallery which looks like this (the page utilizes my plugin). The white space is configurable, images fit perfectly in row and their width/height ratio is persisted. I can share details in the evening - do not have enough time to describe it at the moment. Just please reply if that is what you are looking for. – Anton Jan 28 '16 at 07:40
  • will take a look, but as I see at the bottom you still have 2 missing pictures. – SergkeiM Jan 28 '16 at 07:42
  • @Froxz, good remark however that is intended behavior - I wanted the last line to be like that - you can change it so all lines are full stretched. Just be prepared for a math lesson ;). Or I can share the whole plugin but I have to clean it up - was developed in hurry years ago and is quite messy. – Anton Jan 28 '16 at 07:43
  • I hope you will share you code, when you have time)))) – SergkeiM Jan 28 '16 at 07:49
  • @Froxz, sorry for being late, I was quite busy yesterday - please see my updated post. I haven't had a chance to clean up and publish on Github or anywhere else but in the updated answer you can find a link to jsfiddle which contains all the code you need. Hope it helps. – Anton Jan 29 '16 at 16:10
  • Thank you a lot will try it, seems perfect – SergkeiM Jan 30 '16 at 14:57
  • Hey @Froxz, finally I have published a plugin. Many bug fixes and improvements were added since I posted my previous fiddle. I Hope it helps - see details in the updated answer. – Anton Feb 05 '16 at 11:48
  • thank you, but I went more simple, output only 30 images and resize them width = containerWidth/6, height = containerWidth/5 – SergkeiM Feb 05 '16 at 12:05
2

I created something that might interest you

var container = $('#container');
var height = container.outerHeight();
var width = container.outerWidth();

function populate(n){
    var rem_items = n;
    var rows = Math.round(Math.sqrt(n));
    var row_items = Math.ceil(n/rows);

    for (var i=0; i<rows; i++){

        // this prevents us from generating a lonely single box in a row
        if( (rem_items%(rows-i))===0 ){
            row_items = rem_items/(rows-i);
        }

        if(rem_items<row_items){
            row_items = rem_items;
        }

        rem_items = rem_items-row_items;

        for (var j=0; j<row_items; j++){
            var img_height = height/rows;
            var img_width = width/row_items;
            var img_left = j*img_width;
            var img_top = i*img_height;
            var img = $('<div class="cell"></div>');
            img.css({
                width: img_width,
                height: img_height,
                left: img_left,
                top: img_top
            });
            container.append(img);
        }
    }
}

populate(40);

https://jsfiddle.net/jLq4hgaa/1/

Basically, it calculates the "most balanced" distribution of the images horizontally and vertically.

It does what you're asking for in the plainest sense. It distributes images/containers inside a container evenly regardless of aspect ratio.

DigitalDouble
  • 1,747
  • 14
  • 26
0
$(document).on("pageload",function(){
  $('.container').addClass('stretch');
});

Then make a css element called "stretch" defining width:100%

Height:100% and if need be define layout, i.e relative

Star's Studio
  • 97
  • 1
  • 10