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="content">
   <div class="layer background background-animation">
     <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/217233/pc1.png" />
   </div>
   <div class="layer train train-animation">
     <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/217233/pc2.png" />
   </div>
   <div class="layer foreground foreground-animation">
     <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/217233/pc3.png" />
   </div>
   <div class="frame"></div>
 </div>
              
            
!

CSS

              
                /* Press "Play Animation" to restart/replay */
* {
  box-sizing: border-box;
}

body {
  background-color: rgb(197, 217, 255);
  overflow-x: auto;
}

.content {
  position: relative;
  height: 200px;
  width: 200px;
  margin: 60px auto;
}

.layer {
  position: absolute;
}

.background {
  transform: translateX(0);
}

.train {
  transform: translateX(-440px);
}

.foreground {
  transform: translateX(-210px);
}

.background-animation {
  animation: background-shift 4s linear 1 forwards;
}

.train-animation {
  animation: train-shift 4s linear 1 forwards;
}

.foreground-animation {
  animation: foreground-shift 4s linear 1 forwards;
}

@keyframes background-shift {
  to {
    transform: translateX(-80px);
  }
}

@keyframes train-shift {
  to {
    transform: translateX(-140px);
  }
}

@keyframes foreground-shift {
  to {
    transform: translateX(-400px);
  }
}

/* Just for demostration, not required for animation */

.frame {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  border: 3px solid rgb(0, 255, 21);
}

.bordered {
  border: 2px dotted red;
}

              
            
!

JS

              
                // JS only necessary for deconstruction demo. It is a CSS-only animation
const gui = new dat.GUI({ name: "My GUI" });
const content = document.getElementsByClassName("content")[0];
const layers = document.getElementsByClassName("layer");
const images = document.getElementsByTagName("img");
const replaying = false;

const config = {
  overflow: true,
  exploded: false,
  replay: replay,
  reset: reset,
};

gui
  .add(config, "overflow")
  .name("Overflow?")
  .onChange((newValue) => {
    if (newValue === false) {
      content.style.overflow = "hidden";
    } else {
      content.style.overflow = "visible";
    }
  });

gui
  .add(config, "exploded")
  .name("Explode?")
  .onChange((newValue) => {
    if (newValue === true) {
      const overFlowCheckbox = document.querySelector(
        "div.dg.main li:first-child input"
      );
      overFlowCheckbox.checked = true;
      overFlowCheckbox.disabled = true;

      content.style.overflow = "visible";
      for (element of layers) {
        element.style.position = "static";
      }
    } else {
      config.overflow = false;
      const overFlowCheckbox = document.querySelector(
        "div.dg.main li:first-child input"
      );
      if (overFlowCheckbox.checked === true) {
        overFlowCheckbox.click();
      }

      overFlowCheckbox.disabled = false;
      for (element of layers) {
        element.style.position = "absolute";
      }
    }
    toggleBorders();
  });

gui
  .add(config, "replay")
  .name("Play Animation")
  .onChange((newValue) => {
    replay();
  });

function replay() {
  reset();

  setTimeout(() => {
    layers[0].classList.add("background-animation");
    layers[1].classList.add("train-animation");
    layers[2].classList.add("foreground-animation");
  }, 20);
}

gui
  .add(config, "reset")
  .name("Reset Animation")
  .onChange((newValue) => {
    reset();
  });

function reset() {
  layers[0].classList.remove("background-animation");
  layers[1].classList.remove("train-animation");
  layers[2].classList.remove("foreground-animation");
}

function toggleBorders() {
  for (img of images) {
    img.classList.toggle("bordered");
  }
}

              
            
!
999px

Console