Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2><a href="https://packery.metafizzy.co">Packery v2</a></h2>

<div class="dialog-container">
  <div class="dialog is-waiting">
    <p class="dialog__text">&nbsp;</p>
    <div>
    <button class="button try-again-button">Try again</button>
    <button class="button close-dialog-button">Close</button>
    </div>
  </div>
</div>

<div class="grid">
  <div data-tile="a" class="tile h2 bgx3 bgy1"></div>
  <div data-tile="b" class="tile bgx2 bgy1"></div>
  <div data-tile="c" class="tile bgx1 bgy4"></div>
  <div data-tile="d" class="tile bgx1 bgy1"></div>
  <div data-tile="e" class="tile w2 bgx2 bgy4"></div>
  <div data-tile="f" class="tile h2"></div>
  <div data-tile="g" class="tile bgx3"></div>
  <div data-tile="h" class="tile bgx3 bgy3"></div>
  <div data-tile="i" class="tile bgx1 bgy3"></div>
  <div data-tile="j" class="tile h2 bgy3"></div>
  <div data-tile="k" class="tile h2 bgx2 bgy2"></div>
  <div data-tile="l" class="tile w2 bgy2"></div>
  <div data-tile="m" class="tile w2 bgx1"></div>
</div>

<div>
  <button class="button shuffle-button">Shuffle</button>
</div>
              
            
!

CSS

              
                * { box-sizing: border-box; }

/* force scroll bar */
html { overflow-y: scroll; }

body {
  font-family: sans-serif;
  background: #DDD;
  color: #555;
  text-align: center;
  padding-bottom: 50px;
}

a { color: #19F; }

a:hover { color: #C25; }


.grid {
  width: 400px;
  background: #333;
  margin: 50px auto;
  min-height: 500px;
  z-index: 2;
}

.tile {
  float: left;
  width: 100px;
  height: 100px;
/*   border: 1px solid; */
  background-color: white;
  background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/metafizzy-logo-400.png');
  cursor: move;
}

.tile.w2 { width: 200px; }
.tile.h2 { height: 200px; }

.tile.bgx1 { background-position-x: -100px; }
.tile.bgx2 { background-position-x: -200px; }
.tile.bgx3 { background-position-x: -300px; }

.tile.bgy1 { background-position-y: -100px; }
.tile.bgy2 { background-position-y: -200px; }
.tile.bgy3 { background-position-y: -300px; }
.tile.bgy4 { background-position-y: -400px; }

.tile:hover {
  outline: 5px solid hsla(210, 100%, 50%, 0.7);
  outline-offset: -5px;
  z-index: 2;
}

.tile.is-dragging,
.tile.is-positioning-post-drag {
  outline: none;
  z-index: 2;
  opacity: 0.8;
}

.packery-drop-placeholder {
  outline: 3px dashed white;
  outline-offset: -6px;
  transition: transform 0.2s;
}

/* -------- */

.dialog-container {
  width: 600px;
  margin: 0 auto;
  position: relative;
}

.dialog {
  position: absolute;
  padding: 20px;
  width: 100%;
  background: #333;
  color: white;
  border-radius: 20px;
  text-align: center;
  font-size: 28px;
  box-shadow: 0 5px 10px hsla(0, 0%, 0%, 0.4);
  z-index: 10;
  transition: opacity 0.2s, transform 0.2s;
}

.dialog.is-waiting {
  pointer-events: none;
  opacity: 0;
  transform: translateY(100px);
}


.dialog b {
  color: #EA0;
}

.button {
  font-size: 20px;
  background-color: #19F;
  background-image: linear-gradient( hsla(0, 0%, 0%, 0), hsla(0, 0%, 0%, 0.2) );
  border: none;
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
}

.button:hover {
  cursor: pointer;
  background-color: #3AF;
}

.button:active {
  background-color: #C25;
  box-shadow: inset 0 2px 10px hsla(0, 0%, 0%, 0.5);
}

              
            
!

JS

              
                var grid = document.querySelector('.grid');
var pckry = new Packery( grid, {
  itemSelector: '.tile',
  columnWidth: 100,
  transitionDuration: '0.3s'
});

pckry.getItemElements().forEach( function( itemElem ) {
  var draggie = new Draggabilly( itemElem );
  pckry.bindDraggabillyEvents( draggie );
});

// map items by their data-tile
var mappedItems = {};

pckry.items.forEach( function( item ) {
  var attr = item.element.getAttribute('data-tile');
  mappedItems[ attr ] = item;
});

( function() {

var orders = [
  'abcdefghijklm',
  'ecdibmhfajkgl',
  'ilckfgdebhjam'
];
var d = Object.keys( Packery.defaults ).sort( function( a, b ) {
  return b < a ? 1 : -1;
});
var i=3,j=9,o=Packery.namespace;
var orderIndex = 0;

function shuffleTiles() {
  // shuffle items
  orderIndex++;
  var order = orders[ orderIndex % 3 ];
  pckry.items = order.split('').map( function( attr ) {
    return mappedItems[ attr ];
  });
  // stagger transition
  pckry._resetLayout();
  pckry.items.forEach( function( item, i ) {
    setTimeout( function() {
      pckry.layoutItems( [ item ] );  
    }, i * 34 );
  });
}

var dialog = document.querySelector('.dialog');
var didWin = false;

function win() {
  if ( !didWin ) {
    document.querySelector('.dialog__text').innerHTML = 'Nice work!<br>';
  }
  didWin = true;
  showDialog();
}


function showDialog() {
  dialog.classList.remove('is-waiting');
}

function hideDialog() {
  dialog.classList.add('is-waiting');
}

dialog.querySelector('.try-again-button').onclick = function() {
  hideDialog();
  shuffleTiles();
}

dialog.querySelector('.close-dialog-button').onclick = hideDialog;

document.querySelector('.shuffle-button').onclick = shuffleTiles;

pckry.on( 'dragItemPositioned', function() {
  var order = pckry.items.map( function( item ) {
    return item.element.getAttribute('data-tile');
  }).join('');
  if ( pckry.maxY == 500 && order == 'fmgdbalkjihce' ) {
    win();
  }
});

})();
              
            
!
999px

Console