Parallax Effect Background in JavaScript

Parallax Effect Background in JavaScript
Code Snippet:parallax header Vanilla JS
Author: Cheshire Cat JS
Published: January 20, 2024
Last Updated: January 22, 2024
Downloads: 558
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code creates a background parallax effect for the website header. It adjusts image and content positions based on scrolling. The method `windowScroll()` manipulates the header’s image and content using `translate3d` with varying speeds to create a parallax effect. This effect enhances the visual appeal while users scroll through the web page.

How to Create Parallax Effect Background In Javascript

1. First of all, load the Reset CSS by adding the following CDN link into the head tag of your HTML document.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">

2. Set up the HTML structure. Define a header section with two main divisions: one for the header image and another for the header content.

<div class="header">
  <div class="header__image" id="ParallaxImage" style="background-image: url('https://unsplash.it/1600/850?image=776');"></div>
  <div class="header__content" id="ParallaxContent">
    <h1>Parallax header</h1>
    <h4>Simple vanilla javascript</h4>
  </div>
</div>
<div class="copy">
  <h2>Example Title</h2>
  <h3>Quisque ullamcorper venenatis velit congue.</h3>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer porta sagittis urna eu vulputate. Vestibulum ut turpis sem. Duis vitae nisl lobortis, suscipit dolor non, congue massa. Sed sit amet lectus at neque suscipit luctus. Nulla lorem metus, placerat id aliquet ac, facilisis sed dolor. Nulla quam felis, interdum eget pulvinar et, varius vitae dolor. Maecenas eget mi nec purus tempor fringilla a eget elit. Nunc in laoreet ligula, non cursus arcu. Maecenas vel semper est, ac vehicula ipsum.</p>
  <p>Nullam malesuada mi vehicula, semper lacus at, pharetra mi. Donec fringilla urna nunc, quis convallis ex imperdiet at. Sed lobortis rutrum nisi. Fusce feugiat varius facilisis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras dignissim, massa a pharetra pharetra, elit lectus aliquam urna, eu condimentum dui felis in quam. Quisque vestibulum semper pretium. Sed lacus massa, rutrum vel mauris a, dignissim imperdiet lacus. Quisque semper porttitor ex eu suscipit. Duis malesuada lacus in eleifend ullamcorper.</p>
  <p>Nullam sed tortor vel nulla placerat imperdiet. Suspendisse quis nisi ut neque sodales semper non nec metus. Integer sit amet lacus nibh. Fusce malesuada molestie dui, vitae cursus lacus pulvinar suscipit. Morbi iaculis orci ac quam vestibulum, sit amet dapibus sapien egestas. Praesent sed nisl felis. Aliquam erat volutpat. Fusce quis elit nibh. Phasellus maximus imperdiet turpis vel ultrices. Etiam laoreet metus leo. Nam at facilisis orci. Donec efficitur sem ac posuere elementum. Pellentesque consectetur aliquam lobortis. Pellentesque consectetur aliquam lobortis.</p>
</div>

3. Apply necessary styles to your elements using CSS. This includes setting up the layout and design of the header and other sections on your webpage.

*, *:before, *:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  background-color: #f2f2f2;
}

.header h1 {
  font-family: "Playfair Display", sans-serif;
  font-size: 110px;
  font-weight: 900;
  line-height: 1.1;
  color: #fff;
  margin: 0 0 20px 0;
}

.header h2 {
  font-family: "Playfair Display", sans-serif;
  font-size: 70px;
  font-weight: 900;
  color: #262626;
  margin: 0 0 10px 0;
}

h3 {
  font-family: "Lato", sans-serif;
  font-size: 24px;
  font-weight: 300;
  color: #262626;
  margin: 0 0 60px 0;
}

.header h4 {
  font-family: "Lato", sans-serif;
  font-size: 28px;
  font-weight: 300;
  color: #fff;
  margin: 0 0 0 0;
}

.header {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

.header__image {
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 25;
}

.header__content {
  text-align: center;
  position: relative;
  z-index: 50;
}

.copy {
  text-align: center;
  padding: 60px 25px 55px 25px;
  width: 675px;
  margin: 0 auto;
}

p {
  font-family: sans-serif;
  font-size: 18px;
  font-weight: 300;
  line-height: 1.95;
  color: #262626;
  margin: 0 0 30px 0;
}

4. Finally, add the JavaScript code for the parallax effect. Include the following JavaScript code within a <script> tag just before the closing </body> tag:

function windowScroll() {
  var parallaxImage = document.getElementById('ParallaxImage'),
  parallaxContent = document.getElementById('ParallaxContent'),
  windowScrolled = window.pageYOffset || document.documentElement.scrollTop,
  speedImage = 4,
  speedContent = 6;

  parallaxImage.style.transform = 'translate3d(0, ' + windowScrolled / speedImage + 'px, 0)';
  parallaxContent.style.transform = 'translate3d(0, ' + windowScrolled / speedContent + 'px, 0)';
}

window.addEventListener('scroll', windowScroll);

Feel free to adjust the speedImage and speedContent values to achieve the desired parallax scrolling effect. Experiment with different images and content to make your website even more visually appealing.

That’s all! hopefully, you have successfully created a Parallax Effect in JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X