What is masonry-layout?
The masonry-layout npm package is a JavaScript library for creating dynamic, grid-based layouts. It arranges elements vertically, positioning each element in the next open spot in the grid, much like a mason fitting stones in a wall. This layout is often used for image galleries, portfolios, and other content-heavy websites.
What are masonry-layout's main functionalities?
Basic Grid Layout
This feature allows you to create a basic grid layout. The code initializes a Masonry instance on a container element with the class 'grid' and specifies the item selector and column width.
const Masonry = require('masonry-layout');
const grid = document.querySelector('.grid');
const msnry = new Masonry(grid, {
itemSelector: '.grid-item',
columnWidth: 200
});
Responsive Layout
This feature enables a responsive layout. The code uses a grid-sizer element to define column width and sets percentPosition to true for percentage-based positioning.
const Masonry = require('masonry-layout');
const grid = document.querySelector('.grid');
const msnry = new Masonry(grid, {
itemSelector: '.grid-item',
columnWidth: '.grid-sizer',
percentPosition: true
});
Adding Items Dynamically
This feature allows you to add items dynamically to the grid. The code creates a new grid item, appends it to the grid, and then informs Masonry about the new item using the appended method.
const Masonry = require('masonry-layout');
const grid = document.querySelector('.grid');
const msnry = new Masonry(grid, {
itemSelector: '.grid-item',
columnWidth: 200
});
const newItem = document.createElement('div');
newItem.className = 'grid-item';
grid.appendChild(newItem);
msnry.appended(newItem);
Other packages similar to masonry-layout
isotope-layout
Isotope is a JavaScript library for creating dynamic, filterable, and sortable grid layouts. It offers more advanced features compared to masonry-layout, such as filtering and sorting of grid items. It is also more customizable and has a larger community and more extensive documentation.
packery
Packery is another grid layout library that offers a more flexible and customizable layout compared to masonry-layout. It allows for draggable and resizable grid items, making it suitable for more interactive and complex layouts.
muuri
Muuri is a versatile grid layout library that combines the features of Masonry, Packery, and Isotope. It offers drag-and-drop, filtering, sorting, and responsive layouts. Muuri is highly customizable and provides a comprehensive API for advanced use cases.
Masonry
Cascading grid layout library
Masonry works by placing elements in optimal position based on available vertical space, sort of like a mason fitting stones in a wall. You’ve probably seen it in use all over the Internet.
For complete docs and demos, see masonry.desandro.com or masonryjs.com if you're lazy,
Install
A packaged source file includes everything you need to use Masonry.
Or, if you're cool with the command line...
Install with Bower: bower install masonry
Install with npm npm install masonry-layout
Initialize
In JavaScript
var container = document.querySelector('#container');
var msnry = new Masonry( container, {
itemSelector: '.item',
columnWidth: 200
});
In HTML
Add a class of js-masonry
to your element. Options can be set in JSON in data-masonry-options
.
<div class="js-masonry" data-masonry-options='{ "itemSelector": ".item", "columnWidth": 200 }'>
<div class="item"></div>
<div class="item"></div>
...
</div>
License
Masonry is released under the MIT license. Have at it.
Copyright :copyright: 2015 David DeSandro