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

              
                <div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

<div class="controls">
  <p class="title">Unoptimized</p>
  <p><i>(optimized version <a href="https://codepen.io/chofsho/pen/bZpOGG" target="_blank">here</a>)</i></p>
  <p>Turn on Chrome DevTools and throttle the CPU 6x.</p>
  <button id="start" class="btn" disabled>Start</button>
  <br>
  <button id="stop" class="btn">Stop</button>
</div>


              
            
!

CSS

              
                .box {
  background: lightsalmon;
  height: 30px;
  margin: 0.5rem 0;
  width: 30px;
}

.controls {
  background: #fff;
  box-shadow: 0px 5px 60px 2px rgba(#000, 0.1);
  font-family: Helvetica, sans-serif;
  max-width: 230px;
  padding: 0.25rem 1.5rem 1.5rem;
  position: fixed;
  right: 1.25rem;
  text-align: center;
  top: 1.25rem;
}

.title {
  font-size: 1.5rem;
  font-weight: bold;
  margin-bottom: -0.5rem;
}

.btn {
  background: #c4cbff;
  border-color: #2f44ff;
  display: inline-block;
  font-family: inherit;
  font-size: 1.2rem;
  margin: 0.25em 0;
  min-width: 6.5rem;
  padding: 0.5em 1em;
  
  &:disabled {
    opacity: 0.5;
  }
}

body {
  overflow-x: hidden;
}
              
            
!

JS

              
                /**************************************
  Initialize and set state 
***************************************/

let requestID;
let state = {
  boxes: document.querySelectorAll('.box'),
  data: {}
}

state.boxes.forEach((box, i) => {
  state.data[i] = {
    direction: 'forward',
    oldPosition: i * (Math.random() * 150)
  }
})

moveBox = moveBox.bind(state);


/**************************************
  Set callback for requestAnimationFrame
***************************************/

function moveBox() {
  this.boxes.forEach((box, i) => {  // ⚠️ thrash repeatedly in quick succession
    let data = this.data[i];

    // 1) Move box
    if (data.direction === 'forward') {
      newPosition = data.oldPosition += 5;
    }
    else {
      newPosition = data.oldPosition -= 5;
    }

    box.style.marginLeft = `${newPosition}px`; // ⚠️ write to DOM, invalidating previous snapshot


    // 2) Determine next direction
    let boxPosition = box.getBoundingClientRect().x; // ⚠️ read from DOM, forcing Layout because previous snapshot was invalidated
    let boxWidth    = box.offsetWidth;
    let windowWidth = window.innerWidth;
    let boxHitRightBoundary = boxPosition + boxWidth >= windowWidth;
    let boxHitLeftBoundary  = boxPosition <= 0;

    if (boxHitRightBoundary) {
      data.direction = 'backward';
    }
    else if (boxHitLeftBoundary) {
      data.direction = 'forward';
    }
  })

  // 3) Call requestAnimationFrame again to loop
  requestID = requestAnimationFrame(moveBox);
}



/**************************************
  Start animation
***************************************/
requestAnimationFrame(moveBox);


/**************************************
  Set button click handlers 
***************************************/

let startButton = document.getElementById('start');
let stopButton  = document.getElementById('stop');

startButton.addEventListener('click', startAnimation);
stopButton.addEventListener('click', stopAnimation);


function startAnimation() {
  requestAnimationFrame(moveBox);
  stopButton.removeAttribute('disabled');
  this.setAttribute('disabled', 'true');
}

function stopAnimation() {
  window.cancelAnimationFrame(requestID);
  startButton.removeAttribute('disabled');
  this.setAttribute('disabled', 'true');
}
              
            
!
999px

Console