Simple Parallax Effect in JavaScript

Simple Parallax Effect in JavaScript
Code Snippet: Simple parallax pure javascript with fullPage (pureJS)
Author: Renaud ROHLINGER
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 2,470
License: MIT
Edit Code online: View on CodePen
Read More

This simple JavaScript code snippet helps you to create a parallax effect on scrolling. It gets the starting position of each element to have parallax applied to it. You just need to add a class name “parallax-background” to element on which you want to apply parallax scrolling animation. Then, parallax scrolling function will be called whenever window is scrolled or resized.

You can set the custom values for the scrolling speed and outer height of the elements.

How to Create Simple Parallax Effect in JavaScript

1. First of all, create the HTML structure for parallax effect as follows:

 <div id="fullpage">
  <div class="section">
    <h1>Hey, scroll please</h1>
  </div>
  <div class="section">
    <h1>Hey i'm scrolling in a normal way</h1>
  </div>
  <div class="section">
    <h1>Hey i'm scrolling in a normal way</h1>
  </div>
</div>
 <div class="parallax-background">
    <h1>Hey i'm a parallax content !</h1>
    <h1>Hey i'm a parallax content !</h1>
    <h1>Hey i'm a parallax content !</h1>
  </div>

2. After that, add the following CSS styles into your project:

body {
  text-align: center;
}

.section {
  height: 100vh;
  padding: 2em;
}
.section:nth-of-type(2), .section:nth-of-type(4) {
  background-color: rgba(133, 133, 133, 0.3);
}

.parallax-background {
  position: absolute;
  width: 100%;
  z-index: -1;
  margin-top: -100vh;
}
.parallax-background h1 {
  height: 100vh;
  padding-bottom: 50vh;
  font-size: 5vmax;
  opacity: 0.6;
}

3. Finally, add the following JavaScript code and done.

var windowHeight = window.innerHeight;

document.addEventListener('resize', function() {
	windowHeight = window.innerHeight;
})

function outerHeight(el) {
	var height = el.offsetHeight;
	var style = getComputedStyle(el);

	height += parseInt(style.marginTop) + parseInt(style.marginBottom);
	return height;
}

function parallax (el, speedFactor, outerHeight) {
	var foo = document.querySelectorAll(el);

	var getHeight;
	var firstTop;
	var paddingTop = 0;

	//get the starting position of each element to have parallax applied to it		
	foo.forEach(function(subEl){
	    firstTop = subEl.getBoundingClientRect().top;
	});

	if (outerHeight) {
		getHeight = function(el) {
			return outerHeight(el);
		};
	} else {
		getHeight = function(el) {
			return el.clientHeight;
		};
	}

	// function to be called whenever the window is scrolled or resized
	function update(){
		var pos = window.scrollY;				

		foo.forEach(function(subEl){
			var element = subEl;
			var top = element.getBoundingClientRect().top;
			var height = getHeight(element);
      
			element.style.top = -(Math.round((firstTop - pos) * speedFactor)) + "px";
		});
	}		
	document.addEventListener('scroll', update, true)
	document.addEventListener('resize', update)
	update()
};

parallax(".parallax-background", -0.6);


// Based on  https://github.com/alvarotrigo/fullPage.js/tree/master/pure%20javascript%20(Alpha)
// Use with caution, still in alpha. I'm trying to avoid jquery ^_^
fullpage.initialize('#fullpage', {
    css3:false,
    scrollBar:true
});

That’s all! hopefully, you have successfully integrated this simple parallax effect into your project. If you have any questions or facing any issues, 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