cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <h1>Packery - fit item in previous position, vanilla JS</h1>

<p>Click item to toggle size and fit in place</p>

<div class="grid">
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
</div>

            
          
!
            
              * { box-sizing: border-box; } 

body { font-family: sans-serif; }

/* ---- grid ---- */

.grid {
  background: #DDD;
  max-width: 1200px;
}

/* clear fix */
.grid:after {
  content: '';
  display: block;
  clear: both;
}

/* ---- .grid-item ---- */

.grid-item {
  float: left;
  width: 200px;
  height: 200px;
  background: #C09;
  border: 2px solid hsla(0, 0%, 0%, 0.5);
}

.grid-item:hover {
  background: #C90;
  cursor: pointer;
}

.grid-item--width2 { width: 400px; }
.grid-item--height2 { height: 400px; }

.grid-item--large {
  width: 400px;
  height: 400px;
  background: #0C9;
  z-index: 2;
}

            
          
!
            
              var grid = document.querySelector('.grid');

var pckry = new Packery( grid, {
  itemSelector: '.grid-item',
  // columnWidth useful for fitting
  columnWidth: 200
});

grid.addEventListener( 'click', function( event ) {
  // filter for grid-item clicks
  var target = event.target;
  if ( !target.classList.contains('grid-item') ) {
    return;
  }
  target.classList.toggle('grid-item--large');
  var isLarge = target.classList.contains('grid-item--large');
  var item = pckry.getItem( target );
  if ( isLarge ) {
    // save previous position
    item.previousPosition = {
      x: item.position.x,
      y: item.position.y
    };
    pckry.fit( target );
  } else {
    if ( item.previousPosition ) {
      // fit back in previous position
      pckry.fit( target, item.previousPosition.x, item.previousPosition.y );
      // remove previous position
      delete item.previousPosition;
    } else {
      pckry.shiftLayout();
    }
  }
});

            
          
!
999px
Loading ..................

Console