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 - animate responsive item size</h1>

<p>Click items to toggle size</p>

<div class="grid">
  <div class="grid-sizer"></div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
  <div class="grid-item">
    <div class="grid-item-content"></div>
  </div>
</div>

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

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

body { font-family: sans-serif; }

.grid {
  background: #EEE;
}

/* item is invisible, but used for layout */
.grid-item,
.grid-sizer {
  width: 20%;
}

.grid-item {
  float: left;
  height: 100px;
}

/* grid-item-content is visible, and transitions size */
.grid-item-content {
  width: 100%;
  height: 100%;
  background: #C09;
  border: 2px solid hsla(0, 0%, 0%, 0.5);
  -webkit-transition: width 0.4s, height 0.4s;
          transition: width 0.4s, height 0.4s;
}

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

/* both item and item content change size */
.grid-item.is-expanded {
  width: 60%;
  height: 200px;
  z-index: 2;
}

.grid-item.is-expanded .grid-item-content {
  background: #0C9;
}

            
          
!
            
              // external js: packery.pkgd.js

var $grid = $('.grid').packery({
  itemSelector: '.grid-item',
  percentPosition: true
});

$grid.on( 'click', '.grid-item-content', function( event ) {

  var itemContent = event.currentTarget;
  setItemContentPixelSize( itemContent );

  var itemElem = itemContent.parentNode;
  var $item = $( itemElem );
  var isExpanded = $item.hasClass('is-expanded');
  $( itemElem ).toggleClass('is-expanded');

  // force redraw
  var redraw = itemContent.offsetWidth;
  // renable default transition
  itemContent.style[ transitionProp ] = '';

  addTransitionListener( itemContent );
  setItemContentTransitionSize( itemContent, itemElem );

  if ( isExpanded ) {
    // if shrinking, shiftLayout
    $grid.packery('shiftLayout');
  } else {
    // if expanding, fit it
    $grid.packery( 'fit', itemElem );
  }
});

var docElem = document.documentElement;
var transitionProp = typeof docElem.style.transition == 'string' ?
    'transition' : 'WebkitTransition';
var transitionEndEvent = {
  WebkitTransition: 'webkitTransitionEnd',
  transition: 'transitionend'
}[ transitionProp ];

function setItemContentPixelSize( itemContent ) {
  var previousContentSize = getSize( itemContent );
  // disable transition
  itemContent.style[ transitionProp ] = 'none';
  // set current size in pixels
  itemContent.style.width = previousContentSize.width + 'px';
  itemContent.style.height = previousContentSize.height + 'px';
}

function addTransitionListener( itemContent ) {
  // reset 100%/100% sizing after transition end
  var onTransitionEnd = function() {
    itemContent.style.width = '';
    itemContent.style.height = '';
    itemContent.removeEventListener( transitionEndEvent, onTransitionEnd );
  };
  itemContent.addEventListener( transitionEndEvent, onTransitionEnd );
}

function setItemContentTransitionSize( itemContent, itemElem ) {
  // set new size
  var size = getSize( itemElem );
  itemContent.style.width = size.width + 'px';
  itemContent.style.height = size.height + 'px';
}

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

Console