I think we all agree that the parallax effect can get you that WOW factor when someone visits your website. So, I thought i would show you a live jQuery parallax example. In this tutorial i will explain in detail how to create your own parallax background effect using jQuery to manage the animation aspects of the banner which you could use for your header background.
Update: The Parallax Plugin demo now works on jQuery 1.6.4+. I have updated this post, the demo and new download package to include working functionality with the new version of jQuery. Happy parallaxing!
You will need the background images for the parallax banner. I have chosen to go with 4 images to keep it basic. I have chosen the following 4 images (note that images 2,3,4 are transparent png’s):
- Image layer1: The main background – green vector background
- Image layer2: Overlay image – the frog
- Image layer3: Overlay image – the grass
- Image layer4: Overlay image – the butterflies
Here a few websites where you can get free vector background images:
Note: To edit vector files you will need an editor such as Adobe Illustrator/Photoshop.
You will need the jparallax plugin, the jquery.event.frame and latest version of jQuery – include this in your source code. *The jparrallax.js plugin may already contain jquery.event.frame.js methods, if so you can remove it from the includes below.
Now we add the images to the html code using the following markup structure into your body tag:
<div class="parallax-layer" style="width:1200px; height:250px;">
<img src="images/grass.png" />
<div class="parallax-layer" style="width:500px; height:250px;">
<img src="images/frog2.png" />
<div class="parallax-layer" style="width:1200px; height:300px;">
<img src="images/butterflies3.png" />
Then you add the jQuery code to initialise the parallax plugin into your head tag:
Also add the css style required for the parallax:
position:relative; overflow:hidden; width:950px; height:250px;
position: relative; /* relative, absolute, fixed */
Children of a parallaxed element become layers, and are automatically given position:absolute; in order to start moving them around, but the parallaxed element itself needs position:relative; or position:absolute; or the layers will move relative to the document rather than the viewport. overflow:hidden; stops layers displaying outside of the bounds of the viewport, and width and height should be set to prevent the viewport collapsing.
Tip: play around with the layer image dimensions to get the animation speeds that you want. The smaller the image compared to the background layer the faster it will move when the mouse hovers. The frog only moves left and right, this is achieved by having the same height as the window (background element) but a smaller width. The butterflies is the opposite effect and move when the mouse goes up and down.
That’s pretty much it. Hope you have fun creating your own jQuery animated parallax banners!