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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="wrap--main">
  <h1 class="intro__header">Perfomant CSS animation for beginners</h1>
  
  <div class="grid-r">
    <div class="grid-u-sm-1 grid-u-md-1-2">
      <div class="box"><img src="https://fillmurray.com/200/200" alt=""></div>
      <div class="btn__wrap">
        <button id="js-opacity" class="btn">Opacity</button>
        <button id="js-translate" class="btn btn-two">Translate</button>
        <button id="js-scale" class="btn btn-three">Scale</button>
        <button id="js-rotate" class="btn btn-four">Rotate</button>
      </div>
    </div>
    
    <div class="intro__wrap grid-u-sm-1 grid-u-md-1-2">
      <p class="intro__p">As a general rule, there are only four properties which you can animate whilst maintaining a silky smooth experience for your users. They are: Opacity, Translate, Scale & Rotation. You'll be surprised what you can achieve with only these parameters. Animating 'fixed' elements can result in a screen 're-paint' and a 'janky' transition.<br /> Here, I've chosen to use jQuery to toggle classes because it's eaiser for begininers to read, but I would recommend handling such simple animations with 'vanilla' Javascript. <br />
      <a href="https://twitter.com/IHazelton" class="follow">@IHazelton</a></p>

  <div class="code__wrap">
    <code><pre>

/* the box in its original state*/
.box {
  transition: all 300ms;
}

/* with opacity class */
.box.removeOpacity {
  opacity: 0;
}

/* with translate class */
.box.moveX {
  transform: translateX(100px);
}

/* with scale class */
.box.scale {
   transform: scale(0.4);
}

/* with rotate class */
.box.rotate {
  transform: rotate(45deg);
}

</pre></code>
  </div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto+Mono:300,400,700');

$bodyBg: #fff;
$codeBg: #cacaca;
$textPrimary: #333;
$btnActive: rgba(0,0,0,0.5);
$btnTwo: #2ecc71;
$btnThree: #e74c3c;
$btnFour: #2980b9;

body {
  background-color: $bodyBg;
}
  
.wrap--main {
  padding: 0 5rem;  
}

.intro__header {
  color: $textPrimary;
  font-family: 'Roboto Mono', monospace;
  font-size: 2.4rem;
  line-height: 1.3;
  margin: 1.8rem;
  padding: 3rem;
  text-align: center;
}

.intro__p {
  color: $textPrimary;
  font-size: 1.6rem;
  font-family: 'Roboto Mono', monospace;
  padding: 0 2rem;
    @include breakpoint(md) {
    margin-top: 2rem;
  }
}
 
.follow {
  font-weight: bold;
  &:hover {
    text-decoration: underline;
  }
}

.box {
  background-color: $textPrimary;
  height: 20rem;
  margin: 5rem auto;
  transition: all 300ms;
  width: 20rem;
  &.--opac {
    opacity: 0;
  }
  &.--move {
    transform: translateX(50vw);    
  }
  &.--scaled {
    transform: scale(0.4);
  }
  &.--turn {
    transform: rotate(45deg);
  }
}  
    
.btn__wrap {
  align-items: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
  
.btn {
  color: $bodyBg;
  background-color: #9b59b6;
  cursor: pointer;
  font-family: 'Roboto Mono', monospace;
  font-weight: bold;
  margin: .75rem 0;
  padding: 1.5rem 0;
  width: 20rem;
  &.--active {
    background-color: $btnActive;
  } 
  &-two {
    background-color: $btnTwo;
  } 
  &-three {
    background-color: $btnThree;
  } 
  &-four {
    background-color: $btnFour;
  }
} 
    
.code__wrap {
  background-color: $codeBg;
  color: $textPrimary;
  font-size: 1.3rem;
  margin: 0 auto;
  padding-left: 2rem;
}
  
              
            
!

JS

              
                $('.btn').click(function(e){
    $(this).toggleClass('--active');
});

$('#js-opacity').click(function(e){
  e.preventDefault();
  $('.box').toggleClass('--opac');
});

$('#js-translate').click(function(e){
  e.preventDefault();
  $('.box').toggleClass('--move');
});

$('#js-scale').click(function(e){
  e.preventDefault();
  $('.box').toggleClass('--scaled');
});

$('#js-rotate').click(function(e){
  e.preventDefault();
  $('.box').toggleClass('--turn');
});

              
            
!
999px

Console