This JavaScript code snippet helps you to create a parallax moving effect on the hover event. It gets the page clienX and clientY values on the mouseover event and moves the targeted element accordingly. You can uses this parallax example to smoothly move two elements around the cursor with the parallax effect.
How to Create Parallax Effect on Hover JavaScript
1. First of all, create the HTML structure as follows:
<div class="box parallaxed"> <h1 class="parallaxed">CodeHim - Web Design Code</h1> <svg class="box_image parallaxed" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="196.7 380.2 227 131.5"> <style> .st0{fill:#D6DF22;} .st1{fill:#222221;stroke:#222221;stroke-width:2.500000e-004;stroke-miterlimit:10;} .st2{fill:#222221;stroke:#222221;stroke-miterlimit:10;} </style> <g> <g> <path id="aText" class="st0" d="M197.2 449.6v-6.7c.8-.3.5-1 .4-1.5.3-2.2.5-4.4.9-6.6 3.3-17.1 12.1-30.7 26.2-40.9 9.2-6.7 19.6-10.7 31-11.6h13.8c1 0 2 .1 3 .1.2.3.5.4.9.3 1.4.3 2.9.5 4.3.8l.1.1h.2c21 5.4 35.8 18.1 44.6 37.7 2.8 6.3 4.1 12.9 5 19.7 0 .3.1.6.1.9l1.2 12.3c.1.2.2.4.3.7 0 1.2 0 2.4.2 3.6 2.3 16.5 4.8 33 10.9 48.7 0 .2 0 .3-.1.5-.6-.1-1.2-.2-1.7-.4-9.8-3.4-18.4-8.6-25.5-16.2-1.5-1.6-2.1-1.4-3.5 0-10.1 10.3-22.2 16.8-36.6 19.1-.4-.1-.8 0-1 .4-6.1.5-12.1.7-18.2 0-.4-.4-.8-.4-1.3-.3-1.4-.2-2.9-.5-4.3-.7-.3-.1-.6-.2-.8-.3-2.3-.7-4.7-1.2-6.9-2.1-25.5-10.2-39.9-28.9-42.9-56.1.1-.6.4-1.2-.3-1.5zm65.5-35.5c-18 0-32.5 14.4-32.5 32.3 0 17.8 14.6 32.3 32.6 32.3 17.6 0 32.3-14.7 32.4-32.4.1-17.5-14.7-32.2-32.5-32.2z"/> <animate fill="freeze" calcMode="linear" accumulate="none" attributeName="fill" begin="logo.mouseover" from="#222221" xlink:href="#aText" to="#d6df22" id="changeColor" dur="2s" repeatCount="1" restart="whenNotActive" additive="replace"/> <animate fill="freeze" calcMode="linear" accumulate="none" attributeName="fill" begin="logo.mouseover+2s" from="#d6df22" xlink:href="#aText" to="#222221" id="changeColor2" dur="2s" repeatCount="1" restart="whenNotActive" additive="replace"/> </g> <path class="st1" d="M423.2 479.3c-8.8-5.1-17.7-10.2-26.5-15.3-1.3-.8-1.9-.5-2.6.7-2.1 3.2-4.9 5.7-8 8-15.6 11.3-38.1 6.9-48.4-9.4-1.6-2.5-2.6-5.4-4.2-8 0 1.6.2 3.2.4 4.8 2.1 15.8 4.8 31.6 10.6 46.6.5 1.3 1.1 1.9 2.4 2.2 3.2.9 6.3 1.6 9.5 2.2.2 0 .5 0 .7.1h.4c.5 0 .9.1 1.3.2.4 0 .8.1 1.1.1.4-.1 1 .3 1.5.2h.5c.3 0 .6 0 .9.1 1.9 0 3.9-.1 5.8-.1h1.4c.5-.1.9-.1 1.4-.1 17.5-1.5 32.3-8.8 44.3-21.5 3-3.1 5.3-6.7 7.8-10.1 0-.3-.1-.6-.3-.7z"/> <path id="cText2" class="st2" d="M360.4 380.9c.5-.1 1-.1 1.5-.1h7.3c12.9 1.1 24.9 4.9 35.5 12.5 6.8 4.9 12.5 10.8 17.2 17.8.9 1.3 1 2.1-.6 3-8.4 4.7-16.7 9.5-25 14.3-1.3.7-1.9.7-2.8-.6-11.6-15.5-32.3-18.8-48-7.6-7.5 5.4-11.5 12.9-12.6 22-.1.7-.2 1.3-.3 2.5-1-1.8-.7-3.3-1-4.7-.3-1.4-.3-2.9-.5-4.3-2-11.5-6.6-21.9-14.1-30.8-1-1.2-1-2 .2-3.2 11.7-12.4 26.2-19.2 43.2-20.8z"/> <animate fill="freeze" calcMode="linear" accumulate="none" attributeName="fill" begin="logo.mouseover" from="#222221" xlink:href="#cText" to="#727071" id="changeColor" dur="2s" repeatCount="1" restart="whenNotActive" additive="replace"/> <animate fill="freeze" calcMode="linear" accumulate="none" attributeName="fill" begin="logo.mouseover+2s" from="#727071" xlink:href="#cText" to="#222221" id="changeColor2" dur="2s" repeatCount="1" restart="whenNotActive" additive="replace"/> <animate fill="freeze" calcMode="linear" accumulate="none" attributeName="fill" begin="logo.mouseover" from="#222221" xlink:href="#cText2" to="#727071" id="changeColor" dur="2s" repeatCount="1" restart="whenNotActive" additive="replace"/> <animate fill="freeze" calcMode="linear" accumulate="none" attributeName="fill" begin="logo.mouseover+2s" from="#727071" xlink:href="#cText2" to="#222221" id="changeColor2" dur="2s" repeatCount="1" restart="whenNotActive" additive="replace"/> </g> </svg>
2. After that, add the following CSS styles to your project:
@import url(https://fonts.googleapis.com/css?family=Oswald:700); .box { position: relative; position: absolute; width: 50%; left: 25%; top: 150px; height: 300px; border: 20px solid #fff; box-sizing: border-box; transition: all 0.2s linear; } .box:after { content: ""; position: absolute; z-index: 10; left: 0; right: 0; bottom: -20px; height: 20px; background-color: #fff; } .box:before { content: ""; position: absolute; z-index: 10; left: -20px; top: -20px; bottom: -20px; width: 20px; background-color: #fff; } .box h1 { position: absolute; top: 15px; text-align: center; color: #fff; text-shadow: 0px 0px 5px red; width: 100%; text-transform: uppercase; font-size: 36px; font-weight: 900; line-height: 1; letter-spacing: 5px; z-index: 9; transition: all 0.3s linear; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .box_image { position: absolute; top: -90px; left: 25px; width: 550px; z-index: 2; text-shadow: 0px 0px 5px red; width: 100%; transition: all 0.1s linear; } @media only screen and (max-width: 1024px) { .box { width: 80%; left: 10%; top: 75px; } }
3. Finally, add the following JavaScript code and done.
var b = document.getElementsByTagName("BODY")[0]; b.addEventListener("mousemove", function(event) { parallaxed(event); }); function parallaxed(e) { var amountMovedX = (e.clientX * -0.3 / 8); var amountMovedY = (e.clientY * -0.3 / 8); var x = document.getElementsByClassName("parallaxed"); var i; for (i = 0; i < x.length; i++) { x[i].style.transform='translate(' + amountMovedX + 'px,' + amountMovedY + 'px)' } }
That’s all! hopefully, you have successfully integrated this parallax effect code snippet 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.