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 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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
              <header class="header">
  Web Animations Demo
</header>

<div class="box" id="box"></div>
            
          
!
            
              * {
  margin: 0;
  padding: 0;
}

body {
  background-color: #FFFDF8;
  
}

.header {
  background-color: #80C7D2;
  border-bottom: 4px solid #3E888C;
  padding: 20px;
  color: #1C1208;
  font-size: 18pt;
  font-weight: bold;
  font-family: "Gill Sans", "Gill Sans MT";
}

.box {
  width: 100px;
  height: 100px;
  background-color: #E15E5F;
  position: absolute;
}




            
          
!
            
              /*
 * This is a demonstration of the Web Animations API. 
 * It uses the latest version of web-animations-next.min.js 
 * (v 2.1.2 at the time of writing). Feel free to fork and use this
 * as a starting point for one of your own projects
 */

window.onload = function() {

  var box = document.getElementById("box");
  
  /* 
   * We'll start by moving the box to the middle of the page.
   * First, let's get the coordinates
   */
  var x = ((window.innerWidth/2) - (box.offsetWidth/4));
  var y = ((window.innerHeight/2) - (box.offsetHeight));
    
  /*
   * To animate the box, we'll need to create a KeyframeEffect().
   * This allows us to animate between a set of specific coordinates,
   * or key frames. A KeyframeEffect takes 3 arguments:
   *
   *    element - an object to animate
   *
   *    keyframes - An array of keyframe values to animate between. Keyframes
   *        are objects that contain the properties you want to
   *        animate as keys, and the values you want to animate between
   *        as the values. In this case, we are changing the marginLeft
   *        from 0 to the centered width, and the marginTop from 0 to
   *        the centered height.
   *
   *    AnimationEffectTiming - The AnimationEffectTiming object controls
   *        the way the properties are animated. In this case, we want the
   *        duration to last a total of 1 second (1000ms). We want it to
   *        stay put after it finishes animating, so we'll set the "fill"
   *        property to "forwards"
   */
  var animCenter = new KeyframeEffect(
    box, 
    [
      {marginLeft:0, marginTop:0},
      {marginLeft:x, marginTop:y}
    ], 
    {duration:1000, fill:"forwards"}
  );
  
  /*
   * At this point, we've only defined the animation. Now we have to play it!
   * To do this, we simply reference the document's timeline and tell it 
   * to play our animation
   */
  document.timeline.play(animCenter);
  

}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console