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.

            
              <div class="m-flip__container">
    <p>Click the box</p>
    <div class="m-flip__wrapper">
        <div class="m-flip__box small growing js-flip">
        
        </div>
    </div>
</div>

            
          
!
            
              .m-flip__container {
  width: 100%;
  height: auto;
  font-family: 'Open Sans', sans-serif;
  p {
    margin: 50px auto;
    width: 100px;
  }
}

.m-flip__wrapper {
  width: 400px;
  height: 200px;
  display: block;
  margin: 0 auto;
  position: relative;
}

.m-flip__box {
  background-color: lightblue;
  position: absolute;
  &.small {
    width: 50px;
    height: 50px;
    bottom: 0;
  }
  &.big {
    height: 200px;
    width: 200px;
    left: 200px;
  }
}

.growing {
  transform-origin: bottom left;
  &.transition {
    transition: transform 800ms ease;
  }
}
            
          
!
            
              let elem = document.getElementsByClassName('js-flip'); // store the element

  elem[0].addEventListener('click', function(){ // elem[0] to get the first element (for multiple elements use for loop).

    if (this.classList.contains('small')) {
     
      /*  FIRST: Store the initial size and position  */
      let small = this.getBoundingClientRect();

      /*  LAST: Store the size and position like it would be when the the animation is complete  */
      this.classList.add('growing');
      this.classList.remove('small');
      this.classList.add('big');
      let big = this.getBoundingClientRect();
      this.classList.remove('big');
      this.classList.add('small');
      this.classList.add('transition');

      /*  INVERT: Calculate differences and store them in variables  */
      let invertedLeft = big.left - small.left;
      let invertedWidth = big.width / small.width;
      let invertedHeight = big.height / small.height;

      /*  PLAY: Add transform and release the animation  */
      this.style.transform = 'translateX(' + invertedLeft + 'px) translateZ(0) scale(' + invertedWidth + ', ' + invertedHeight + ')';

      this.addEventListener('transitionend', function handler() {
        this.classList.remove('transition');
        this.style.transform = '';
        this.classList.remove('growing');
        this.classList.remove('small');
        this.classList.add('big');
        this.removeEventListener('transitionend', handler);
      });

    } else {
      this.classList.add('small');
      this.classList.remove('big');
    }

  });
            
          
!
999px
Loading ..................

Console