MiniMasonry.js Documentation

repository·master·Indexed 19 days ago

https://github.com/spope/minimasonry.js

A lightweight, dependency-free JavaScript library for creating masonry layouts. Version 1.3.2 uses CSS transforms and GPU acceleration for smooth animations. It includes a configurable API for managing gutters, sorting directions, and layout triggers, with built-in throttled window resize handling.

Tokens
718
Snippets
3
Records
6
Agent score
16%

What's inside MiniMasonry.js

  1. How MiniMasonry handles window resizing

    master
    MiniMasonry automatically adds a resize event listener to the window to redraw the masonry layout whenever the window size changes. This listener is throttled to 66ms (approximately 15fps) to ensure performance.
  2. Import MiniMasonry into your project

    master

    Depending on your project setup, include MiniMasonry using a <script> tag for minified builds or an import statement for ESM.

    <!-- Using minified version -->
    <script src="node_modules/minimasonry/build/minimasonry.min.js"></script>
    
    <!-- Using ESM -->
    <script type="module">
    import MiniMasonry from "minimasonry";
    </script>
  3. How to use MiniMasonry for masonry layouts

    master

    To implement a masonry layout, ensure your HTML structure follows these requirements:

    1. The container must be relatively positioned (position: relative).
    2. The children elements must be absolutely positioned (position: absolute).

    Initialize the layout by creating a new instance of MiniMasonry and passing the container selector or element.

    var masonry = new MiniMasonry({
        container: '.masonry_transition'
    });
  4. Configure MiniMasonry parameters

    master

    When initializing new MiniMasonry(options), you can pass the following configuration parameters:

    NameDefaultDescription
    containernullRequired. Container's selector or HTMLElement.
    baseWidth255Target width of elements (int).
    gutter10Width/height of gutter between elements (int).
    gutterXnullWidth of gutter between elements. Requires gutterY to work; otherwise falls back to gutter.
    gutterYnullHeight of gutter between elements. Requires gutterX to work; otherwise falls back to gutter.
    minifytrueIf true, places elements on the shortest column. If false, keeps exact order of the list.
    surroundingGuttertrueSet left gutter on first columns and right gutter on last.
    ultimateGutter5Gutter applied when only 1 column can be displayed.
    direction"ltr"Sorting direction: "ltr" or "rtl".
    wedgefalseIf true, starts from left/right according to direction. If false, starts from center.
  5. MiniMasonry API reference

    master

    The MiniMasonry instance provides the following methods:

    • layout(): Triggers a relayout of the masonry if the list of elements has changed.
    • destroy(): Removes the window resize listener and restores the container to its original state.