### Bower Installation for Masonry Source: https://context7.com/desandro/masonry/llms.txt Provides instructions for installing Masonry using the Bower package manager, which is typically used in legacy front-end projects. ```bash bower install masonry-layout --save ``` -------------------------------- ### NPM Installation and Usage Source: https://context7.com/desandro/masonry/llms.txt Details how to install Masonry using npm for modern JavaScript build workflows and how to import and initialize it within a JavaScript module. ```bash npm install masonry-layout --save ``` ```javascript // Import in your JavaScript module import Masonry from 'masonry-layout'; var msnry = new Masonry('.grid', { columnWidth: 60, itemSelector: '.grid-item' }); ``` -------------------------------- ### CDN Installation for Masonry Source: https://context7.com/desandro/masonry/llms.txt Provides instructions for including Masonry directly from a CDN (unpkg) for quick integration into web projects. It shows both the packaged (minified) and unminified versions for development purposes. ```html ``` -------------------------------- ### Install Masonry via CDN Source: https://github.com/desandro/masonry/blob/master/README.md Include Masonry directly into your HTML using CDN links. Choose either the un-minified or minified version for your project. This method is straightforward for quick integration. ```html ``` -------------------------------- ### Accessing Masonry Instance and Invoking Methods Source: https://context7.com/desandro/masonry/llms.txt Illustrates how to get a reference to the Masonry instance after initialization to call its methods, such as 'layout()'. It also shows how to access the instance when initialized with jQuery. ```javascript // Store reference during initialization var msnry = new Masonry('.grid', { columnWidth: 60 }); // Call methods on instance msnry.layout(); // jQuery: access via data attribute var msnry = $('.grid').data('masonry'); msnry.layout(); ``` -------------------------------- ### Initialize Masonry with Stamps in JavaScript Source: https://github.com/desandro/masonry/blob/master/sandbox/stamps.html Demonstrates how to initialize Masonry instances in JavaScript, applying grid layout to specified containers. This example showcases the use of `itemSelector`, `columnWidth`, `gutter`, and `stamp` options for layout configuration. ```javascript var container = document.querySelector('#alpha'); var msnry = new Masonry( container, { itemSelector: '.item', columnWidth: 60, gutter: 10, stamp: '.stamp' }); ``` ```javascript var container = document.querySelector('#beta'); var msnry = new Masonry( container, { columnWidth: 60, gutter: 10, stamp: '.stamp' }); ``` -------------------------------- ### Masonry Basic RTL Initialization (JavaScript) Source: https://github.com/desandro/masonry/blob/master/sandbox/right-to-left.html Initializes a Masonry instance with right-to-left layout support. It targets a container element and sets `originLeft` to false for RTL behavior. Requires the Masonry library. ```javascript ( function() { var container = document.querySelector('#basic'); var msnry = new Masonry( container, { originLeft: false, columnWidth: 60 }); })(); ``` -------------------------------- ### Initialize Masonry Grid and Add Items (JavaScript) Source: https://github.com/desandro/masonry/blob/master/sandbox/add-items.html Initializes a Masonry grid layout on an element with the class 'grid'. It configures column width using a '.grid-sizer' element and enables percentage-based positioning. It also includes event listeners for buttons to prepend and append new items to the grid dynamically. Dependencies: Masonry library. ```javascript function getItemElement() { var elem = document.createElement('div'); var wRand = Math.random(); var hRand = Math.random(); var widthClass = wRand > 0.8 ? 'grid-item--width3' : wRand > 0.6 ? 'grid-item--width2' : ''; var heightClass = hRand > 0.8 ? 'grid-item--height3' : hRand > 0.5 ? 'grid-item--height2' : ''; elem.className = 'grid-item ' + widthClass + ' ' + heightClass; return elem; }; var msnry = new Masonry( '.grid', { columnWidth: '.grid-sizer', percentPosition: true }); document.querySelector('.prepend-button').addEventListener( 'click', function() { var itemElem = getItemElement(); msnry.element.insertBefore( itemElem, msnry.element.firstChild ); msnry.prepended( itemElem ); }); document.querySelector('.append-button').addEventListener( 'click', function() { var itemElem = getItemElement(); msnry.element.appendChild( itemElem ); msnry.appended( itemElem ); }); ``` -------------------------------- ### Initialize Masonry with Horizontal Order (JavaScript) Source: https://github.com/desandro/masonry/blob/master/sandbox/horizontal-order.html Initializes a new Masonry instance with the 'horizontalOrder' option set to true. This option arranges items from left to right. Requires the Masonry library to be included. ```javascript new Masonry( '.grid--1', { itemSelector: '.grid-item', horizontalOrder: true, }); new Masonry( '.grid--2', { itemSelector: '.grid-item', horizontalOrder: true, }); ``` -------------------------------- ### Masonry Basic Bottom Up Layout (JavaScript) Source: https://github.com/desandro/masonry/blob/master/sandbox/bottom-up.html Initializes Masonry with a bottom-up layout. Sets 'isOriginTop' to false to arrange items starting from the bottom. Requires a container element with the ID 'basic'. ```javascript ( function() { var container = document.querySelector('#basic'); var msnry = new Masonry( container, { isOriginTop: false, columnWidth: 60 }); })(); ``` -------------------------------- ### Masonry Stamped RTL Initialization (JavaScript) Source: https://github.com/desandro/masonry/blob/master/sandbox/right-to-left.html Initializes a Masonry instance with right-to-left layout and a stamped element. It configures item selection, RTL orientation, column width, gutter, and the stamped element's selector. Requires the Masonry library. ```javascript ( function() { var container = document.querySelector('#stamped'); var msnry = new Masonry( container, { itemSelector: '.item', originLeft: false, columnWidth: 60, gutter: 10, stamp: '.stamp' }); })(); ``` -------------------------------- ### Initialize Masonry with Horizontal Order and Stamp (JavaScript) Source: https://github.com/desandro/masonry/blob/master/sandbox/horizontal-order.html Initializes a Masonry instance with 'horizontalOrder' enabled and specifies a 'stamp' element. The stamp element will not be included in the masonry layout. Requires the Masonry library. ```javascript new Masonry( '.grid--3', { itemSelector: '.grid-item', horizontalOrder: true, stamp: '.stamp', }) ``` -------------------------------- ### Masonry Dynamic Item Addition Source: https://context7.com/desandro/masonry/llms.txt Adds new items to the Masonry layout dynamically using `prepended()` for new items at the start or `appended()` for new items at the end. Requires event listeners to trigger item addition. ```javascript var msnry = new Masonry('.grid', { columnWidth: '.grid-sizer', percentPosition: true }); // Prepend items document.querySelector('.prepend-button').addEventListener('click', function() { var itemElem = document.createElement('div'); itemElem.className = 'grid-item'; msnry.element.insertBefore(itemElem, msnry.element.firstChild); msnry.prepended(itemElem); }); // Append items document.querySelector('.append-button').addEventListener('click', function() { var itemElem = document.createElement('div'); itemElem.className = 'grid-item'; msnry.element.appendChild(itemElem); msnry.appended(itemElem); }); ``` -------------------------------- ### Initialize Masonry with Vanilla JavaScript Source: https://context7.com/desandro/masonry/llms.txt Initializes Masonry using vanilla JavaScript, either with an element reference or a CSS selector string. Requires the Masonry library to be loaded. ```javascript var container = document.querySelector('.grid'); var msnry = new Masonry(container, { itemSelector: '.grid-item', columnWidth: 60 }); var msnry = new Masonry('.grid', { columnWidth: 60 }); ``` -------------------------------- ### Initialize Masonry with Vanilla JavaScript Source: https://github.com/desandro/masonry/blob/master/README.md Initialize Masonry using native JavaScript. This can be done by passing either a DOM element or a CSS selector to the Masonry constructor, along with layout options. ```javascript // init with element var grid = document.querySelector('.grid'); var msnry = new Masonry( grid, { // options... itemSelector: '.grid-item', columnWidth: 200 }); // init with selector var msnry = new Masonry( '.grid', { // options... }); ``` -------------------------------- ### Initialize Masonry with HTML Attributes Source: https://context7.com/desandro/masonry/llms.txt Declaratively initializes Masonry using data attributes directly in HTML, eliminating the need for JavaScript initialization code. Requires the Masonry library script to be included. ```html