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="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"></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. Anythings else, including 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>
            
          
!
            
              $bodyBg: #34495E;
$codeBg: #10253A;
$textPrimary: #fff;
$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-size: 2.4rem;
  line-height: 1.3;
  margin: 1.8rem;
  padding: 3rem;
  text-align: center;
}

.intro__p {
  color: $textPrimary;
  font-size: 1.6rem;
  padding: 0 2rem;
    @include breakpoint(md) {
    margin-top: 2rem;
  }
}
 
.follow {
  color: $btnTwo;
  &:hover {
    text-decoration: underline;
  }
}

.box {
  background-color: $textPrimary;
  height: 10rem;
  margin: 5rem auto;
  transition: all 300ms;
  width: 10rem;
  &.--opac {
    opacity: 0;
  }
  &.--move {
    transform: translateX(100px);    
  }
  &.--scaled {
    transform: scale(0.4);
  }
  &.--turn {
    transform: rotate(45deg);
  }
}  
    
.btn__wrap {
  align-items: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
  
.btn {
  color: $textPrimary;
  background-color: #9b59b6;
  border-radius: .4rem;
  cursor: pointer;
  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;
}
  
            
          
!
            
              $('.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
Loading ..................

Console