Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

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.

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

              
                <div class="container">
  <div class="child">
    <div class="content">
      <span>One</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Two</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Three</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Four</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Five</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Six</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Seven</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Eight</span>
    </div>
  </div>
  <div class="child">
    <div class="content">
      <span>Nine</span>
    </div>
  </div>
</div>

              
            
!

CSS

              
                body {
  margin: 20px;
  background: #f1f1f1;
  font-family: sans-serif;
  font-weight: bold;
}

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.child {
  cursor: pointer;
  position: relative;
  background: black;
  width: calc((100% - 60px) * 0.25);
  margin-bottom: 20px;
  will-change: transform;
  overflow: hidden;
}
.child:after {
  content: " ";
  display: block;
  padding-bottom: 100%;
}

.child.hero {
  width: 100%;
  order: -1;
}
.child.hero:after {
  padding-bottom: calc(9 / 16 * 100%);
}

.content {
  -webkit-font-smoothing: antialiased;
  pointer-events: none;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
}

.content > * {
  transform: translateZ(0);
}

              
            
!

JS

              
                console.clear();

const global = {
  // DOMCollection to array for easier looping
  children: [].slice.call(document.querySelector('.container').children),
  currentHero: null,
  animating: false,
};

global.children.forEach((c) => {
  c.style.background = `hsl(${Math.random() * 360 | 0}, 50%, 50%)`;
  c.addEventListener('click', (e) => { toggleHeroState(e.target) });
  // TweenMax.set(c, {x: '+=0'})
});

function toggleHeroState(newHero) {
  if (global.animating === true) return;
  // if hero item is clicked again, unhero it
  newHero = newHero === global.currentHero ? null : newHero;
  
  animateLayoutChange(newHero, global.currentHero);

  global.currentHero = newHero;
}

function animateLayoutChange(newHero, currentHero) {
  global.animating = true;
  
  // store bounds before css change
  const beforeRects = global.children.map((c) => c.getBoundingClientRect());
  
  // apply css changes
  newHero && newHero.classList.toggle('hero');
  currentHero && currentHero.classList.toggle('hero');
  
  // store bounds after css change
  const afterRects = global.children.map((c) => c.getBoundingClientRect());
  
  // create master timeline
  const tl = new TimelineMax({onComplete: () => {
    // reset transforms on children after animation is complete
    TweenMax.set(global.children, {clearProps: 'transform'});
    
    global.animating = false;
  }});
  
  // tl.timeScale(0.1)
  
  global.children.forEach((c, i) => {
    // get before and after bounds
    const before = beforeRects[i];
    const after = afterRects[i];
    
    // calculate scale/width/height changes based on bounds
    const sx = before.width / after.width;
    const sy = before.height / after.height;
    
    // calculate position/left/top changes based on bounds
    const tx = before.left - after.left + (before.width - after.width) * 0.5;
    const ty = before.top - after.top + (before.height - after.height) * 0.5;
    
    // get the content so we can inverse-scale it (hacky?)
    const content = c.children[0];
    
    // animate!
    tl.from(c, 0.5, {
      ease: Power3.easeOut,
      scaleX: sx, 
      scaleY: sy,
      x: tx,
      y: ty,
      onUpdate: () => {
        TweenMax.set(content, {
          scaleX: 1.0 / c._gsTransform.scaleX,
          scaleY: 1.0 / c._gsTransform.scaleY,
        });
      },
      onComplete: () => {
        TweenMax.set(content, {clearProps: 'transform'});
      }
    }, i * 0.025);
  });
};

              
            
!
999px

Console