cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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.

            
              <!--
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>
            
          
!
            
              @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;
  }
}
            
          
!
            
              // 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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console