Simple Parallax Effect On Scrolling Using jQuery And CSS
Last Updated : Jul 1, 2023
In this tutorial we create a simple parallax effect on scrolling using jquery and css, Parallax Effect is new modern effect in web designing it involves the background moving at a slower rate to the foreground, creating a 3D effect as you scroll down the page.
You may also like Page Scroll To Top Using jQuery
CHECK OUT THIS TUTORIAL LIVE DEMO →
To Create Parallax Effect On Scrolling it takes only one step:-
- Make a HTML file and define markup,script and styling
Step 1. Make a HTML file and define markup,script and styling
We make a HTML file and save it with a name parallax_effect.html
<html> <head> <script src="js/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(window).scroll(function () { var yPos=-($(window).scrollTop() / 6); if($(window).scrollTop()>100) { document.getElementById("div1_wrapper").style.backgroundPosition="100% "+yPos+"px"; } if($(window).scrollTop()<100) { document.getElementById("div1_wrapper").style.backgroundPosition="100% 100%"; } }); </script> <style> body { margin:0px; auto; padding:0px; text-align:center; font-family:helvetica; color:white; } #wrapper { position:relative; } #wrapper #stats { position:fixed; color:white; } #wrapper div { height:700px; } #wrapper div p { margin:0px; text-align:center; padding:100px; padding-top:150px; font-size:50px; } #div1_wrapper { background:url("images/parallax1.jpg") no-repeat; background-attachment:fixed; background-size: 100% 100%; background-repeat: no-repeat; width:100%; } #div2_wrapper { background:url("images/parallax2.jpg") no-repeat; background-attachment:fixed; background-size: 100% 100%; background-repeat: no-repeat; width:100%; } </style> </head> <body> <div id="wrapper"> <p id="stats" style="position:fixed;color:white;"></p> <div id="div1_wrapper"> <div id="div1"> <p>This Is Content 1 and all you page content goes here <br>b style="color:#298A08;">Demo of Parallax Effect Using jQuery and CSS</b> </p> </div> </div> <div id="div2_wrapper"> <div id="div2"> <p>This Is Content 2 and all you page content goes here <b style="color:#298A08;">Demo of Parallax Effect Using jQuery and CSS</b> </p> </div> </div> </div> </body> </html>
In this step we create two div that holds the image and content,each width is 700px and.
Then we use css background property to apply images in divs background and make them fixed to get parallax effect.
Then we use $(window).scroll function to run the function when user scrolls the window and under this function we get how much distance the user scroll the page.
You may also like Load Results From Database On Page Scroll Using jQuery,Ajax And PHP
And then we divide the number by 6 it is the speed to which the background image scroll you can specify any number in place of 6 and also we use (-) sign because to create parallax effect you have to move the image opposite to the scrolling direction and by using negative sign when the user scroll down image moves upward and when scroll up image moves down.
We use 100 in if condition because we need to show parallax after 100px distance from top you can specify any number as you wish.
You may also like animated background using CSS3.
Thats all, this is how to create Parallax Effect On Scrolling Using jQuery and CSS. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on Parallax Effect Using jQuery and CSS helps you and the steps and method mentioned above are easy to follow and implement.