Packery Documentation

repository·master·Indexed 26 days ago

https://github.com/metafizzy/packery

A bin-packing layout library for gapless, draggable grid layouts. Packery allows elements to be arranged with minimal gaps and supports initialization via Vanilla JavaScript, jQuery, or HTML data attributes. It includes features for fitting items dynamically, sorting by position, and integration with Draggabilly or jQuery UI Draggable for interactive reordering.

Tokens
928
Snippets
4
Records
11
Agent score
38%

What's inside Packery

  1. Install Packery via CDN, Download, or Package Managers

    master

    You can include Packery in your project using several methods:

    CDN

    Link directly to the files on unpkg:

    <script src="https://unpkg.com/packery@3/dist/packery.pkgd.js"></script>
    <!-- or minified -->
    <script src="https://unpkg.com/packery@3/dist/packery.pkgd.min.js"></script>

    Package Managers

    • npm: npm install packery --save
    • Bower: bower install packery --save

    Download

    You can download the un-minified packery.pkgd.js or minified packery.pkgd.min.js directly from unpkg.

    npm install packery --save
  2. Initialize Packery using HTML data attributes

    master

    Packery can be automatically initialized by adding a data-packery attribute to your container element. Configuration options can be provided as a JSON string within the attribute value.

    <div class="grid" data-packery='{ "itemSelector": ".grid-item" }'>
      <div class="grid-item"></div>
      <div class="grid-item"></div>
      ...
    </div>
  3. Initialize Packery with Vanilla JavaScript

    master

    You can initialize Packery using the Packery constructor. You can pass either a DOM element or a selector string as the first argument.

    // initialize with element
    var grid = document.querySelector('.grid');
    var pckry = new Packery(grid, {
      // options...
      itemSelector: '.grid-item'
    });
    
    // initialize with selector string
    var pckry = new Packery('.grid', {
      // options...
    });
  4. Fit an item into its current position

    master

    Use the fit() method to expand an element within the layout. Packery will position other items around it to accommodate the change. This is useful for expanding elements dynamically.

    fit(elem, [x], [y])

    • elem: The DOM element to fit.
    • x (optional): The horizontal destination position.
    • y (optional): The vertical destination position.

    When an item is being fitted, Packery emits the fitComplete event once the item and the rest of the layout have finished updating.

  5. Access Packery internal classes

    master

    Packery exposes its internal helper classes via the main constructor:

    • Packery.Rect: Handles rectangle logic and collision detection (canFit).
    • Packery.Packer: The core bin-packing algorithm engine.
    • Packery.Item: Represents an individual item within the layout.