simple-masonry.js
Show items with different heights in a grid of responsive columns.
No position: absolute
and so fully flexible in height and width of any item.
Check demo.
simple-masonry.js is written as es6. If you are not using a es6-compiler in your project then use simple-masonry-compiled.js!
simple-masonry works great with bootstrap (but you can use your own grid as well!). Wrap some columns in a container and fill them with items. It dosn't matter if you put them all in one column or preorder them. simple-masonry will order all items from each column in a zipper method as you can see in the following snippet.
<section class="masonry-box container">
<div class="masonry-column col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="item">
<h3>item 1</h3>
</div>
<div class="item">
<h3>item 4</h3>
</div>
<div class="item">
<h3>item 5</h3>
</div>
<div class="item">
<h3>item 6</h3>
</div>
<div class="item">
<h3>item 7</h3>
</div>
<div class="item">
<h3>item 8</h3>
</div>
</div>
<div class="masonry-column col-lg-3 col-md-4 col-sm-6">
<div class="item">
<h3>item 2</h3>
</div>
</div>
<div class="masonry-column col-lg-3 col-md-4">
<div class="item">
<h3>item 3</h3>
</div>
</div>
<div class="masonry-column col-lg-3"></div>
</section>
Create columns and set the column width with bootstrap classes (or other grid system) like usual.
!important: set 'col-[]-12' classes! Also if it seems to be senseless in normal use.
In default you can use the css-class masonry-box
for the wrapping element and masonry-column
for columns. Items don't need a extra class. Every element will be ordered if it's not just a pure text-node.
Get simple-masonry and include it in your project.
npm
$ npm install simple-masonry -S
bower
$ bower install simple-masonry
You can use your own classes for wrapping element and columns if you put them as parameter.
Usage
var simpleM = new SimpleMasonry();
Or with your own selector.
var simpleM = new SimpleMasonry({
masonryBox: '.my-box-class',
masonryColumn: '.my-column-class'
});
Then run it
simpleM.init();
.append()
simpleM.append(node);
simpleM.append([node1, node2]);
.prepend()
simpleM.prepend(node);
simpleM.prepend([node1, node2]);
.get()
simpleM.get();
simpleM.get(index);
.each()
simpleM.each(function (item, index) {
});
simpleM.each(function (item, index) {
}, columnIndex);
simpleM.get().forEach(function (item, index) {
});
.columnsLength()
simpleM.columnsLength();
.on()
simpleM.on('append', function (item) {
});
simpleM.on('prepend', function (item) {
});
simpleM.on('order', function (items) {
});
Demo and more information here.
Authors