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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.