This JavaScript code creates a multi-layer parallax effect. It uses mouse movement and scrolling to animate various shapes and text elements on the webpage. The code sets up functionalities for both mouse and scroll-based parallax movements. By toggling checkboxes, you can activate parallax motion on mouse movement or scrolling.
The code relies on the position of the mouse cursor or scroll position to create a depth effect, moving elements in different directions based on the user’s actions.
How to Create Multi-Layer Parallax Effect In JavaScript
1. To begin, let’s create the HTML structure for your web page. You can customize the content, but ensure you include the necessary elements for the parallax effect. Replace the sample elements with the <section>
tag with the elements you want to apply the parallax effect to.
<h1>Parallaxe Multiply Layer</h1> <div> <p>Use the Checkboxes to test Scroll or Mouse Parallax</p> <input type="checkbox" id="mm" checked value="mouse move"> <label for ="mm">Parallax on Mouse move</label> <input type="checkbox" id="sm" checked value="scroll move"> <label for ="sm">Parallax on Scroll</label> </div> <section class="container"> <div class="element-wrapper"> Rectangle <div class="image-layer" data-image-layer="tardis"></div> <svg width="100%" height="100%" class="parallaxe-layer"> <rect x="25%" y="25%" width="50%" height="50%" fill="#55F545"/> </svg> </div> <div class="element-wrapper"> Circle <div class="image-layer" data-image-layer="tardis"></div> <svg width="100%" height="100%" class="parallaxe-layer"> <circle cx="50%" cy="50%" r="30%" fill="#55F545" /> </svg> </div> <div class="element-wrapper"> Path <div class="image-layer" data-image-layer="tardis"></div> <svg width="100%" height="100%" class="parallaxe-layer"> <path d="M164.06,212.46C147.19,140.6,0,0,0,0H81.56s-26.78,90.38-15,109.27c13,20.77,82.92,6.74,97.5-12.14C177.32,80,164.06,0,164.06,0h93.75s-14.42,87,0,105.63c7.46,9.65,42.19,0,42.19,0V212.46l-53,68.6-83-.61-15,19.43-67.5-87.42,140.63-98.34L29.06,159V267.1s223.82,74,247.5,0c8-25-59.06-76.49-59.06-76.49S183,265.83,165.94,257.39C157.83,253.36,166.59,223.22,164.06,212.46Z" transform="translate(-3 -2)" fill="#55F545"/> </svg> </div> <div class="element-wrapper"> Polygon <div class="image-layer" data-image-layer="tardis"></div> <svg width="100%" height="100%" class="parallaxe-layer"> <polygon points="150,20 330,190 110,280" fill="#55F545" /> </svg> </div> <div class="element-wrapper"> Text <div class="image-layer" data-image-layer="tardis"></div> <svg width="100%" height="100%" class="parallaxe-layer"> <text x="100" y="10" fill="#55F545" transform="rotate(30 20,40)" font-size="5.5em" font-weight="bold">JUST</text> <text x="40" y="100" fill="#55F545" transform="rotate(30 20,40)" font-size="5.5em" font-weight="bold">TEXT!</text> </svg> </div> <div class="element-wrapper"> Rectangle with Offset + Blur filter <div class="image-layer" data-image-layer="tardis"></div> <svg width="100%" height="100%" class="parallaxe-layer"> <filter id="f1" x="0" y="0" width="200%" height="200%"> <feOffset result="offOut" in="SourceGraphic" dx="30" dy="30" /> <feGaussianBlur result="blurOut" in="offOut" stdDeviation="5" /> <feBlend in="SourceGraphic" in2="blurOut" mode="normal" /> </filter> <rect x="25%" y="25%" width="50%" height="50%" fill="#55F545" stroke="red" stroke-width="6px" filter="url(#f1)" /> </svg> </div> </section> <div class="spacer">for more scrolling</div>
2. Create a CSS file (styles.css) to define the styles for your elements and configure the parallax effect. Here’s a sample CSS code:
* { margin: 0; padding: 0; box-sizing: border-box; vertical-align: middle; } .cd__main{ display: block !important; background: linear-gradient(to right, #e0eafc, #cfdef3) !important; } html, body { height: 100%; } body { text-align: center; padding: 1.5rem; } h1 { display: inline-block; font-weight: normal; border-bottom: thin solid black; margin: 1em; } .container { display: flex; min-height: 15em; flex-wrap: wrap; justify-content: center; } .element-wrapper { margin: 5em; position: relative; width: 20%; height: 20em; } .element-wrapper div { position: absolute; } [data-image-layer] { position: relative; height: 100%; width: 100%; background-size: cover; background-position-x: center; background-blend-mode: multiply; transition: background-color 0.5s; } .parallaxe-layer { mix-blend-mode: multiply; } [data-image-layer=tardis] { background-image: url(https://thenerdstash.com/wp-content/uploads/2015/05/Tardis-HD-2.jpg); } .spacer { min-height: 1000px; } .no-background-blend-mode .parallaxe-layer { opacity: 0.6; }
3. Now, let’s add the JavaScript code for the parallax effect. Create a JavaScript file (script.js) and add the following code:
document.addEventListener("DOMContentLoaded", function(){ initParallax(); document.addEventListener('mousemove', mouseParallax); document.addEventListener('scroll', scrollParallax) }); var cursorX = 0; var cursorY = 0; var offsetX = 0; var offsetY = 0; var depthXY = 0; var scenes; var layers; // Initialize variables function initParallax() { scenes = document.querySelectorAll('.element-wrapper'); layers = document.querySelectorAll('svg'); } // Called on scroll function scrollParallax(event) { if(document.getElementById('sm').checked) { sP(layers, 0.9); } } // Called on Mouse move function mouseParallax(event) { if(document.getElementById('mm').checked) { mP(event); } } //Mouse move Parallax function mP() { cursorX = event.clientX; cursorY = event.clientY; for(i = 0; i<=layers.length; i++){ depthXY = 0.3; offsetX = ((cursorX * depthXY) / 8); offsetY = ((cursorY * depthXY) / 8); layers[i].style.transform = 'translate3d('+offsetX+'px,'+offsetY+'px,0px)'; } } //Scrolling Parallax function sP($object, multiplier){ multiplier = typeof multiplier !== 'undefined' ? multiplier : 0.5; multiplier = 1 - multiplier; var from_top = window.pageYOffset; var bg_css = '0' +(multiplier * from_top) + 'px'; for (var index = 0 ; index < $object.length; index++) { $object[index].style.marginTop = bg_css; } }; //IE Fallback if(typeof window.getComputedStyle(document.body).backgroundBlendMode == 'undefined') { document.documentElement.className += " no-background-blend-mode"; }
You can customize and extend this code to implement your own unique parallax effect on your web projects, providing users with an engaging and visually appealing experience
That’s all! hopefully, you have successfully created a Parallax Effect on your webpage. If you have any questions or suggestions, 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.