Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <script id="boxTmpl" type="x-tmpl-mustache">
  <div id="{{id}}" class="box" {{{bg}}}>
    <div class="back" {{{bg}}}></div>
    <div class="top"></div>
    <div class="bottom"></div>
    <div class="opening"></div>
    <div class="spine" {{{bg}}}></div>
  </div>
</script>

<div id="ctrlLeft" class="ctrl left"><i class="fas fa-angle-left"></i></div>
<div id="ctrlRight" class="ctrl right"><i class="fas fa-angle-right"></i></div>

<div id="preloader"></div>
              
            
!

CSS

              
                $width        : 240px; 
$height       : 480px; 
$spine_width  : 20px; 

html,
body {
  position: relative;
  height: 100%;
	perspective: 1200px;
  overflow: hidden;
} 

body {
  background: #8a8888
}

#preloader {
  position: fixed;
  top: -1px;
  left: -1px;
  width:1px;
  height:1px;
}

.ctrl {
  position:fixed;
  top:50%;
  transform: translateY(-50%);
  background-color: rgba(black, 0.5);
  color: rgba(white, 0.5);
  font-size: 4em;
  width: 64px;
  height:64px;
  display:flex;
  justify-content:center;
  align-items:center;
  cursor: pointer;
  
  &.left { left: 0; z-index: 10; }
  &.right { right: 0; z-index: 11; }
}

.box,
.back {
  position: absolute;
  width: $width;
  height: $height;
  background-size: 2.1 * $width $height;
}

.box {
  background-position: right;
  backface-visibility: hidden;
  top: 50%;
  left: 0;
  transform-style: preserve-3d;
  transform-origin: 0;
  box-shadow: 0 0 30px rgba(black, 0.25);
  
  div {
    position: absolute;
    transform-style: preserve-3d;
    transform-origin: 0;
  }

  .back {
    background: white;
    background-size: 2.1 * $width $height;
    background-position: left;
    transform-origin: 50%;
    transform: translateZ(-$spine_width) rotateY(180deg);
    backface-visibility: hidden;
  }
  
  .opening,
  .spine {
    width: $spine_width;
    height: $height;
    transform: rotateY(90deg);
  }
  
  .opening {
    background: white;
    right: 0;
    transform: rotateY(90deg) translateZ($spine_width);
    backface-visibility: hidden;
  }
  
  .spine {
    background-size: 2 * $width $height;
    background-position: center;
  }
  
  .top,
  .bottom {
    background: #ccc;
    width: $width;
    height: $spine_width;
    backface-visibility: hidden;
  }
  
  .top {
    transform: translate3d(0, -0.5 * $spine_width, -0.5 * $spine_width) rotateX(90deg);
  }
  
  .bottom {
    bottom: 0;
    transform-origin: center;
    transform: translate3d(0, 0.5 * $spine_width, -0.5 * $spine_width) rotateX(-90deg);
  }
}

@keyframes rot {
	0%    { transform: rotateX(25deg)   rotateY(0)        translate(-150px, -50%); }
	50%   { transform: rotateX(-25deg)  rotateY(-180deg)  translate(-150px, -50%); }
	100%  { transform: rotateX(25deg)   rotateY(-360deg)  translate(-150px, -50%); }
}
              
            
!

JS

              
                /**
 * @todo  add all boxes to document, so on next/previous just position 
 *        show and animate box, instead of creating a new element
 */

// Array of cover images
const games = [
  "https://assets.codepen.io/439000/cover2.jpg",
  "https://assets.codepen.io/439000/cover3.jpg",
  "https://assets.codepen.io/439000/cover4.jpg",
  "https://assets.codepen.io/439000/cover5.jpg",
  "https://assets.codepen.io/439000/cover6.jpg",
  "https://assets.codepen.io/439000/cover1.jpg",
  "https://assets.codepen.io/439000/cover7.jpg",
  "https://assets.codepen.io/439000/cover8.jpg",
  "https://assets.codepen.io/439000/cover9.jpg",
  "https://assets.codepen.io/439000/cover10.jpg",
  "https://assets.codepen.io/439000/cover11.jpg",
];

// store template for use with Mustache
const box_tmpl = document.getElementById("boxTmpl").innerHTML;

/**
 *  Shows the next box
 * 
 *  @param [number] increment direction (adds `dir` to index)
 */
const nextBoxExec = (dir) => {
  if (dir === -1) {
    currentIndex = currentIndex > 0 ? currentIndex - 1 : games.length - 1;
  } else if (dir === 1) {
    currentIndex = currentIndex < games.length - 1 ? currentIndex + 1 : 0;
  }

  // calc dimensions
  const dw = Math.max(
    document.documentElement["clientWidth"],
    document.body["scrollWidth"],
    document.documentElement["scrollWidth"],
    document.body["offsetWidth"],
    document.documentElement["offsetWidth"]
  );

  // remove current
  if ("undefined" !== typeof currentBox) {
    // "transfer" currentBox to obsoleteBox
    const obsoleteBox = currentBox;
    if (obsoleteBox.gsap) {
      obsoleteBox.gsap.kill();
      // animate out
      obsoleteBox.gsap = gsap.to(obsoleteBox.transform, {
        duration: 1.0,
        tx: dir === -1 ? dw + 100 : -100,
        rx: obsoleteBox.transform.rx + 180,
        ry: obsoleteBox.transform.ry + 90,
        ease: "back.in",
        onUpdate: () => {
          obsoleteBox.el.style.transform = `translate3d(${obsoleteBox.transform.tx}px, -50%, 0) rotateX(${obsoleteBox.transform.rx}deg) rotateY(${obsoleteBox.transform.ry}deg)`;
        },
        onComplete: () => {
          obsoleteBox.el.parentNode.removeChild(obsoleteBox.el);
        }
      });
    }
  }

  // unique id
  const id = `box-${gid++}`;

  // render box with Mustache
  let html = Mustache.render(box_tmpl, {
    id: id,
    bg: `style="background-image:url('${games[currentIndex]}')"`
  });
  document.body.insertAdjacentHTML("beforeend", html);
  currentBox = { el: document.getElementById(id), data: {} };

  // box width
  const bw = currentBox.el.getBoundingClientRect().width;

  // init position
  const startPos = dir === -1 ? -100 : dw + 100;
  currentBox.el.style.transform = `translate3d(${startPos}px, -50%, 2000px) rotateX(-120deg) rotateY(-180deg)`;

  // animate in
  currentBox.transform = { tx: startPos, rx: -90, ry: 210 };
  currentBox.gsap = gsap.to(currentBox.transform, {
    duration: 2.4,
    tx: 0.5 * dw - 0.5 * bw,
    rx: Math.random() > 0.5 ? -16 : 16,
    ry: Math.random() > 0.5 ? -26 : 26,
    ease: "elastic.out(0.4, 0.3)",
    onUpdate: () => {
      currentBox.el.style.transform = `translate3d(${currentBox.transform.tx}px, -50%, 0) rotateX(${currentBox.transform.rx}deg) rotateY(${currentBox.transform.ry}deg)`;
    }
  });
};

// init
let currentIndex = -1,
  gid = 1,
  currentBox;

// interactions
document
  .getElementById("ctrlRight")
  .addEventListener("click", () => nextBoxExec(1));
document
  .getElementById("ctrlLeft")
  .addEventListener("click", () => nextBoxExec(-1));

// kickoff by triggering right click
document.getElementById("ctrlRight").click();

// preload next
games.forEach((url, index) => {
  if (index === 0) {
    return;
  }
  var link = document.createElement('link');
  link.rel = 'prefetch';
  link.href = url;
  document.head.appendChild(link);  
});

              
            
!
999px

Console