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

              
                <h1>Timed <code>requestAnimationFrame</code></h1>
<p>Click any of the buttos and watch it move down during its given duration</p>
<button data-duration="500">500ms</button>
<button data-duration="2000">2000ms</button>
<button data-duration="5000">5000ms</button>
              
            
!

CSS

              
                
              
            
!

JS

              
                window.addEventListener('DOMContentLoaded', () => {

  // animateOverTime
  // - duration
  // - step callback function
  // - done callback function
  const animateOverTime = (duration = 400, stepCb = () => {}, doneCb = () => {} ) => {
    
    let rafId = null;
    let start = null;
    
    const animate = (timestamp) => {

      if (!start) start = timestamp;

      const progress = (timestamp - start)  / duration;

      if (progress > 1) {
        window.cancelAnimationFrame(rafId);
        doneCb();
        start = null;
      } else {
        stepCb(progress);
        rafId = window.requestAnimationFrame(animate);
      }

    };
    
    rafId = window.requestAnimationFrame(animate);

  };
  
  
  // Hook click events to buttons
  document.querySelectorAll('button').forEach((button) => {

    button.addEventListener('click', (e) => {
    
      // Don't jump
      e.preventDefault();
      e.stopPropagation();

      // Get button itself
      const $this = e.target;
            
      // Prevent Double Clicks
      if ($this.classList.contains('busy')) return false;
      $this.classList.add('busy');
      
      // Animate the element over time
      animateOverTime(

        // Duration: the value of [data-duration] or 400ms by default
        $this.dataset.duration || 400,
        
        // Step Callback: update translateY value
        (progress) => {
          $this.style.transform = `translateY(${progress * 100}%)`;
        },
        
        // Done Callback: clear busy and jump back to initial position
        () => {
          $this.style.transform = '';
          $this.classList.remove('busy');
        }

      );

    });

  });

});
              
            
!
999px

Console