<div class="drag-container"></div>
<div class="grid">
  <div class="item">
    <div class="item-content">1</div>
  </div>
  <div class="item tall">
    <div class="item-content">2</div>
  </div>
  <div class="item small">
    <div class="item-content">3</div>
  </div>
  <div class="item tall">
    <div class="item-content">4</div>
  </div>
  <div class="item">
    <div class="item-content">5</div>
  </div>
  <div class="item tall">
    <div class="item-content">6</div>
  </div>
  <div class="item">
    <div class="item-content">7</div>
  </div>
  <div class="item tall">
    <div class="item-content">8</div>
  </div>
  <div class="item">
    <div class="item-content">9</div>
  </div>
  <div class="item tall">
    <div class="item-content">10</div>
  </div>
  <div class="item">
    <div class="item-content">11</div>
  </div>
  <div class="item tall">
    <div class="item-content">12</div>
  </div>
  <div class="item">
    <div class="item-content">13</div>
  </div>
  <div class="item tall">
    <div class="item-content">14</div>
  </div>
  <div class="item">
    <div class="item-content">15</div>
  </div>
  <div class="item tall">
    <div class="item-content">16</div>
  </div>
  <div class="item">
    <div class="item-content">17</div>
  </div>
  <div class="item tall">
    <div class="item-content">18</div>
  </div>
  <div class="item">
    <div class="item-content">19</div>
  </div>
  <div class="item tall">
    <div class="item-content">20</div>
  </div>
</div>
.drag-container {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
}
.grid {
  position: relative;
}
.item {
  position: absolute;
  width: 200px;
  height: 200px;
  line-height: 200px;
  margin: 5px;
  z-index: 1;
}
.item.small {
  position: absolute;
  width: 200px;
  height: 100px;
  line-height: 100px;
  margin: 5px;
  z-index: 1;
}
.item.tall {
  position: absolute;
  width: 200px;
  height: 300px;
  line-height: 300px;
  margin: 5px;
  z-index: 1;
}
.item.muuri-item-hidden {
  z-index: 0;
}
.item.muuri-item-releasing {
  z-index: 2;
}
.item.muuri-item-dragging {
  z-index: 3;
}
.item-content {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  background: red;
  font-size: 50px;
  color: white;
  cursor: pointer;
}
.item.muuri-item-dragging .item-content {
  background: blue;
}
.item.muuri-item-releasing .item-content {
  background: blueViolet;
}
var grid = new Muuri('.grid', {
  dragEnabled: true,
  dragContainer: document.querySelector('.drag-container'),
  dragAutoScroll: { targets: [ window ] },
  layout: function (grid, layoutId, items, width, height, callback) {
    var layout = {
      id: layoutId,
      items: items,
      slots: [],
      styles: {},
    };

    var item,
      m,
      x = 0,
      y = 0,
      w = 0,
      h = 0;

    for (var i = 0; i < items.length; i++) {
      item = items[i];
      x += w;
      y += h;
      m = item.getMargin();
      w = item.getWidth() + m.left + m.right;
      h = item.getHeight() + m.top + m.bottom;
      layout.slots.push(x, y);
    }

    w += x;
    h += y;

    // Set the CSS styles that should be applied
    // to the grid element.
    layout.styles.width = w + 'px';
    layout.styles.height = h + 'px';

    // When the layout is fully computed
    // let's call the callback function and
    // provide the layout object as it's argument.
    callback(layout);
  },
});
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.3.2/web-animations.min.js
  2. https://cdn.jsdelivr.net/gh/haltu/muuri@0.9.5/dist/muuri.min.js