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>
  <div class="scene">
    <div class="card">
      <div class="card-face card-face-front">FRONT</div>
      <div class="card-face card-face-back">BACK</div>
      <div class="card-face card-face-right">RIGHT</div>
      <div class="card-face card-face-left">LEFT</div>
      <div class="card-face card-face-top">TOP</div>
      <div class="card-face card-face-bottom">BOTTOM</div>
    </div>
  </div>

  <div class="padding"></div>
</div>
              
            
!

CSS

              
                :root {
  --card-max-width: 640px;
  --card-max-height: 300px;
  --card-thickness: 40px;
}

body {
  font-family: Sans-Serif;
  font-size: 2rem;
  font-weight: 900;
}

.padding {
  height: 1000px;
}

.scene {
  /* Arbitrary values, just use the ones that better suits your webpage */
  width: 40vw;
  height: 25vw;
  /* First use of the variables, our scene can't be larger than our card */
  max-width: var(--card-max-width);
  max-height: var(--card-max-height);
  /* Small values of perspective makes it look like we are using a fisheye lens
     while large values places the camera far away as if we were using a tele-objective */
  perspective: 4000px;
  position: fixed;
}

.card {
  width: 100%;
  height: 100%;
  max-width: var(--card-max-width);
  max-height: var(--card-max-height);
  position: relative;
  /* Forces child elements (all faces) to be positioned in 3D space */
  transform-style: preserve-3d;
  /* Our first equations! Read below 👇 */
  transform-origin: 
    /* x-offset */ calc(0.5 * var(--card-max-width))
    /* y-offset */ calc(0.5 * var(--card-max-height)) /* z-offset */
    calc(0.5 * var(--card-thickness));
  /* Just a short transition to make animations smooth */
  transition: transform 0.6s ease-out;
}

.card-face {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-face-front,
.card-face-back {
  /* Front and back faces just fill the horizontal space */
  height: 100%;
  width: 100%;
}

.card-face-front {
  /* Move the front card half the thickness up */
  transform: translateZ(calc(0.5 * var(--card-thickness)));
  background-color: #390099;
}

.card-face-back {
  transform: 
	  /* Rotate the back card in two axis so that it looks good when fully rotated */ rotateY(
      180deg
    )
    rotateZ(180deg) /* Move the back card half the thickness down */
    translateZ(calc(0.5 * var(--card-thickness)));
  background-color: #9e0059;
}

.card-face-left,
.card-face-right {
  width: var(--card-thickness);
  height: var(--card-max-height);
  /* Position left and right faces at the horizontal center of the cube */
  left: calc(0.5 * (var(--card-max-width) - var(--card-thickness)));
}

.card-face-left {
  /* Left side faces left and is placed at the left end of the cube */
  transform: rotateY(-90deg) translateZ(calc(0.5 * var(--card-max-width)));
  background-color: #ff0054;
}

.card-face-right {
  /* Right side faces right and is placed at the right end of the cube */
  transform: rotateY(90deg) translateZ(calc(0.5 * var(--card-max-width)));
  background-color: #ff5400;
}

.card-face-bottom,
.card-face-top {
  width: var(--card-max-width);
  height: var(--card-thickness);
  /* Position left and right faces at the vertical center of the cube */
  top: calc(0.5 * var(--card-max-height) - 0.5 * var(--card-thickness));
}

.card-face-bottom {
  /* Bottom side faces down and is placed at the bottom of the cube */
  transform: rotateX(-90deg) translateZ(calc(0.5 * var(--card-max-height)));
  background-color: #ffbd00;
}

.card-face-top {
  /* Top side faces up and is placed at the bottom of the cube */
  transform: rotateX(90deg) translateZ(calc(0.5 * var(--card-max-height)));
  background-color: #ffbdbd;
}

              
            
!

JS

              
                const SCROLL_UNTIL_FULL_ROTATION_PX = 500;

/**
 * Clamp restricts a certain value X between a Min and Max values.
 * If X < Min then Min is returned
 * If X > Max then Max is returned
 * Else X is returned
 */
const clamp = (min, max, x) => Math.min(max, Math.max(min, x));
/**
 * Lerp stands for Linear Interpolation. Given two values X and Y
 * and normalized value T between 0 and 1, it returns a value between
 * X and Y that is proportional to the value T.
 * If T < 0 then X is returned
 * If T > 1 then Y is returned
 * Else linear interpolation is returned
 */
const lerp = (x, y, t) => clamp(x, y, x * (1 - t) + y * t);

document.addEventListener("DOMContentLoaded", () => {
  const card = document.querySelector(".card");
  let scrollRotation = 0;

  document.addEventListener("scroll", () => {
    scrollRotation = lerp(
      0,
      360,
      window.scrollY / SCROLL_UNTIL_FULL_ROTATION_PX
    );
    card.style.transform = `rotateX(${scrollRotation}deg)`;
  });
});

              
            
!
999px

Console