parallax effect

parallax effect

by Saksham Malhotra

HTML

<div class="parallax">
  <div class="page bg-red">
    <span class="xl white center">Parallax Demo</span>
  </div>
  <div class="page">
    <div class="layer base"><span class="xl white center">&</span></div>
    <div class="layer sub bg-green">
      <span class="xl white top-left tile"><span class="text">Build</span></span>
    </div>

    <div class="layer super">
      <span class="xl white bottom-right tile"><span class="text">Share</span></span>
    </div>
  </div>
  <div class="page bg-blue">
    <span class="xl white center">The End</span>
  </div>
</div>

CSS

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.parallax {
  perspective: 300px;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
}

.page {
  position: relative;
  height: 500px;
  height: 100vh;
  transform-style: preserve-3d;
}

.bg-green {
  background-color: rgba(103, 238, 159);
}

.bg-red {
  background-color: rgba(194, 62, 45);
  z-index: 1
}

.bg-blue {
  background-color: #0071bc;
}

.layer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.layer.sub {
  transform: translateZ(-300px) scale(2.1);
}

.layer.super {
  transform: translateZ(100px) scale(0.67);
}

.xl {
  font-size: 64px;
  font-weight: bold;
}

.white {
  color: white;
}

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.tile {
  height: 30vh;
  width: 30vh;
  border: 10px solid #fff;
  box-sizing: border-box;
  display: block;
  position: absolute;
}

.top-left {
  position: absolute;
  top: 35vh;
  left: 30vh;
}

.bottom-right {
  position: absolute;
  bottom: 35vh;
  right: 30vh;
}

.text {
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  position: absolute;
}