Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!--
A simple, extensible JavaScript-based timeline for use in CSS animation projects. Thsi example uses style changes on a simple circular element, but you can sequentially apply styles to CSS selectors in any order you want by using the JS animation array.
For more information, check out: http://mediatemple.net/blog/tips/building-an-animated-web-project-using-a-javascript-timeline/
-->

<div class="circle  step-1"> <!--start out with .step-1 applied to .circle-->
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p>4</p>
  <p>5</p>
</div>

<button id="run">
  Run animation
</button>

<button id="restart">
  Restart animation
</button>

<a href="http://mediatemple.net/blog/tips/building-an-animated-web-project-using-a-javascript-timeline/">Learn more at the Media Temple blog &rsaquo;</a>
              
            
!

CSS

              
                @color1: #e66705;
@color2: #ff9900;
@color3: #f47762;
@color4: #cccccc;
@color5: #8ebcc1;

// For this Pen, I'm animating the appearance of a circular element.
// With the animation array in the JS section below, you can sequentially
// apply styles to any CSS selector you choose.

.circle {
  width:300px;
  height:300px;
  border-radius:50%;
  position:absolute;
  top:50%;
  left:50%;
  margin:-150px 0 0 -150px;
  box-shadow:inset 0 0 3px rgba(0,0,0,.35);
  transition:background-color .15s linear;
  display:table; // so I can vertically align the child paragraph
  p {
    display:none;
    font-family:helvetica,arial,sans-serif;
    color:#ffffff;
    font-size:60px;
    font-weight:bold;
    text-shadow:0 1px 3px rgba(0,0,0,.5);
  }
}

// This is initial appearance of the element, step-1
.step-1 {
  background-color:@color1;
  p {
    &:nth-child(1){
      display:table-cell;
      vertical-align:middle;
      text-align:center;
    }
  }
}

// These are the styles to apply to the circle when .step-2 is added to .circle
// It's all about carefully overriding specfic styles in a certain order
.step-2 {
  background-color:@color2;
  p {
    &:nth-child(n){ // I need to overwrite all the other nth-child styles...
      display:none; // and make the previous number display:none
    }
    &:nth-child(2){
      display:table-cell;
      vertical-align:middle;
      text-align:center;
    }
  }
}

// Third step
.step-3 {
  background-color:@color3;
  p {
    &:nth-child(n){
      display:none;
    }
    &:nth-child(3){
      display:table-cell;
      vertical-align:middle;
      text-align:center;
    }
  }
}

// Fourth step
.step-4 {
  background-color:@color4;
  p {
    &:nth-child(n){
      display:none;
    }
    &:nth-child(4){
      display:table-cell;
      vertical-align:middle;
      text-align:center;
    }
  }
}

// Fifth step
.step-5 {
  background-color:@color5;
  p {
    &:nth-child(n){
      display:none;
    }
    &:nth-child(5){
      display:table-cell;
      vertical-align:middle;
      text-align:center;
    }
  }
}

// Peripheral styles
// These are just for the demonstration, they don't effect the animation at all

.interaction-text {
  font-family:helvetica,arial,sans-serif;
  font-size:18px;
  color:rgba(0,0,0,.8);
  font-weight:lighter;
}

button {
  background-color:transparent;
  border:2px solid #e8f5f7;
  padding:20px;
  border-radius:4px;
  cursor:pointer;
  cursor:hand;
  outline:none;
  .interaction-text;
  &:hover {
    background-color:#e8f5f7;
  }
  &:active {
    background-color:@color5;
    border-color:@color5;
  }
}

a {
  padding-left:20px;
  .interaction-text;
  text-decoration:none;
  &:hover {
    text-decoration:underline;
  }
}
              
            
!

JS

              
                // This array sets up the animation steps
// time: number of milliseconds before adding the class
// step: the name of the class to add to the element
// selector: the name of the element that you're applying the class to

var animations = [
    {
        // Activate the second step, circle #2
        time:2000,
        step:"step-2",
        selector:'.circle'
    },
    {
        // Activate the third step
        time:2000,
        step:"step-3",
        selector:'.circle'
    },
    {
        // Activate the fourth step
        time:2000,
        step:"step-4",
        selector:'.circle'
    },
    {
        // Activate the fifth step
        time:2000,
        step:"step-5",
        selector:'.circle'
    }
];

// Grab data from the animation array
// and create sets of class additions to elements after defined setTimeouts

function runAnimation(i, timeline){
    setTimeout(function(){
        $(animations[i].selector).addClass(animations[i].step);
    }, timeline);
}

// Go through the array, and execute each step one at a time in order
// by doing runAnimation() on each array element

function animationTimeline(){
    var timeline = 0;

    for(var i=0; i<animations.length; i++){
        // for each step in the animation array
        // update timeline to be value of animations[i].time PLUS previous timeline value
        timeline = parseInt(animations[i].time, 10) + parseInt(timeline, 10);
        runAnimation(i, timeline);
    }
}

// Run the animation
$('#run').click(function(){
    animationTimeline();
});

// Restart the animation
$('#restart').click(function(){
    $('.circle').removeClass("step-2 step-3 step-4 step-5");
    animationTimeline();
});
                   
              
            
!
999px

Console