Packery js shuffle and fit to container

435 views Asked by At

So I have a Packery grid, looking to shuffle items so that they perfectly organize at least to the height of the container...if the width is off, that's fine.

Looking at a layout like this:

<div class="grid">
        <div class="grid-item grid-sizer"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/150x150" border="0" class="img-responsive" /></div>
        <div class="grid-item grid-item--width2"><img src="http://placehold.it/700x250" border="0" class="img-responsive" /></div>
        <div class="grid-item grid-item--width2"><img src="http://placehold.it/700x200" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
        <div class="grid-item grid-item--width2"><img src="http://placehold.it/700x200" border="0" class="img-responsive" /></div>
        <div class="grid-item grid-item--width2"><img src="http://placehold.it/700x200" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/150x150" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
        <div class="grid-item"><img src="http://placehold.it/350x150" border="0" class="img-responsive" /></div>
    </div>

Some general styling:

.grid-sizer, .grid-item{
            width: 20%;
    float: left;

            img{
                width: 100%;
                height: auto;
            }
        }

        .grid-item--width2{
            width: 40%;
        }

Lastly, the initiator:

var $grid = $('.grid').imagesLoaded( function() {
$grid.packery({
    columnWidth: '.grid-sizer',
    itemSelector: '.grid-item',
    gutter: 0,
    precentPosition: true
});

});

https://jsfiddle.net/yfp841un/

1

There are 1 answers

0
jjeining On BEST ANSWER

So I've decided that without some additional jquery which I can write later if I want, there is going to have to be specs on uploads. It's impossible to avoid the gappage without it, if someone wants to blow my mind and show me, without using a stack of jQuery which I know it can be done then.

However, the shuffle, I did write up a piece, and then packery it back up, actually ends up doing pretty darn well most the time...still get some gaps until I standardize my specs though.

var $grid = $('.grid');
var $gridItems = $grid.children('.grid-item');

$gridItems.sort(function(a, b) {
var tmp = parseInt(Math.random() * 10);
var isOddOrEven = tmp % 2;
var isPosOrNeg = tmp > 5 ? 1 : -1;
return (isOddOrEven & isPosOrNeg);
}).appendTo($grid);

shuffle($gridItems).appendTo($grid);

$('.grid').packery();

function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}