Helloween | Background Parallax Effects on Mousemove with parallax.js

10/08/2021

Contents

Demo

Full Screen

Code

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Helloween Background Parallax Effect on Mousemove with JavaScript</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/parallax/3.1.0/parallax.min.js"></script>
  </head>
  <body>
    <div id="container">
      <ul id="scene" data-friction-x="0.1" data-friction-y="0.1" data-scalar-x="25" data-scalar-y="15">
        <li class="layer" data-depth="0.10">
          <div class="background"></div>
        </li>
        <li class="layer" data-depth="0.20">
          <div class="moon"></div>
        </li>
        <li class="layer" data-depth="0.30">
          <div class="castle"></div>
        </li>
        <li class="layer" data-depth="0.15">
          <h2 class="title">Helloween</h2>
        </li>
        <li class="layer" data-depth="0.20">
          <p class="subtitle">Background Parallax Effects<br class="sp"> with parallax.js</p>
        </li>
        <li class="layer" data-depth="0.40">
          <div class="bat-1"></div>
        </li>
        <li class="layer" data-depth="0.50">
          <div class="bat-2"></div>
        </li>
        <li class="layer" data-depth="0.60">
          <div class="bat-3"></div>
        </li>
        <li class="layer" data-depth="0.80">
          <div class="bat-4"></div>
        </li>
        <li class="layer" data-depth="1.00">
          <div class="witch"></div>
        </li>
      </ul>
    </div>
  </body>
</html>

CSS

@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}
body {
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  background-color: #111;
}
#container {
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  width: 100%;
  height: 100vh;
}
#scene {
  position: relative;
  width: 100%;
  height: 100%;
}
.layer {
  display: block;
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}
.background {
  position: absolute;
  top: -2.5%;
  left: -2.5%;
  width: 105%;
  height: 105%;
  background-image: url(bg.png);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.moon {
  position: absolute;
  top: -5%;
  left: -5%;
  width: 110%;
  height: 110%;
  background-image: url(moon.png);
  background-position: 40% -100px;
  background-repeat: no-repeat;
  background-size: 500px auto;
}
.castle {
  position: absolute;
  bottom: -10%;
  left: -10%;
  width: 120%;
  height: 120%;
  background-image: url(castle.png);
  background-position: center bottom;
  background-repeat: no-repeat;
  background-size: cover;
}
.title {
  position: absolute;
  bottom: .8em;
  width: 100%;
  color: #ff7100;
  font-size: 6.25rem;
  text-align: center;
}
.subtitle {
  position: absolute;
  bottom: 1.1em;
  width: 100%;
  color: #fff9de;
  font-size: 2.25rem;
  text-align: center;
}
.bat-1 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(bat_1.png);
  background-position: 47% 52%;
  background-repeat: no-repeat;
  background-size: 25px;
}
.bat-2 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(bat_2.png);
  background-position: 44% 44%;
  background-repeat: no-repeat;
  background-size: 30px;
}
.bat-3 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(bat_3.png);
  background-position: 49% 5%;
  background-repeat: no-repeat;
  background-size: 40px;
}
.bat-4 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(bat_4.png);
  background-position: 81% 0;
  background-repeat: no-repeat;
  background-size: 50px;
}
.witch {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(witch.png);
  background-position: 37% 25%;
  background-repeat: no-repeat;
  background-size: 50px;
}
.sp {
  display: none;
}
@media (max-width: 640px) {
  .title {
    font-size: 5rem;
  }
  .subtitle {
    font-size: 1.75rem;
  }
  .moon {
    background-position: -100px -100px;
    background-size: 400px auto;
  }
  .bat-2,
  .bat-4 {
    display: none;
  }
  .witch {
    background-position: 10% 10%;
  }
}
@media (max-width: 500px) {
  .title {
    bottom: 1.5em;
    font-size: 2.75rem;
  }
  .subtitle {
    bottom: 1em;
    font-size: 1.25rem;
  }
  .moon {
    background-size: 300px auto;
  }
  .sp {
    display: block;
  }
}

JavaScript

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene);