In this article I'll explain what a Parallax is? What does it mean? How to create one from scratch? There're so many JS libraries out there that I used to the effect in action but I never thought of getting into the basics of its What & How. Trust me, it's that simple. Let's move ahead and explore what's in there. This article is useful for all Beginners in Web Design & Development.
What is Parallax?
Definition: The effect whereby the position or direction of an object appears to differ when viewed from different positions.
Well, that's a bit tough to understand what exactly a Parallax mean. Putting it simple, Imagine you're in a forest. You know how when you move sideways and look at the trees, the ones at the distance seem to move at a different rate (slower) than the ones near you?
Another easy way to understand this is to hold your index finger about 6 inches from your face. Look at it with just your right eye, then just your left eye, and notice how its position changes with respect to the
background.
Or, when you play old side-scrolling video games, the backgrounds will have three layers that move at different speeds to make it look like you're moving?
That moving of different layers at different speeds effect is parallax. I hope now you understand what does that heavy word mean? Okay, let's move now and see what composes a parallax.
What is takes to create a Parallax?
Practically a parallax is composed of more than one layer in parallel, moving along on scroll at different speeds, giving us the feel that they're at different distance. You're free to add as many layers to create a parallax but more than one. CSS is there to do all the layers for you and JavaScript will update the CSS values every time you scroll through the contents. That's all it takes to create it. Let's see now how we do it with a demo.
Demo: How we do it?
I will take two layers for demo purposes (you may include as many as you want as I explained earlier).
Here you can see I've two divs having class "background" for background layer and "contents" for the layer on top.
I'm going to include more contents to "contents" div so that we can have the space to scroll. Below is the CSS which does all the magic of creating the layers from the two Divs above:
So, to reveal the secret behind, we fix the layers in background using CSS and later increase/decrease the at a desired rate on scroll. That's all a parallax has to do with it.
Finally, the JavaScript which make the effect come into picture:
Here I've a function named "parallax" which updates the "top" CSS property for fixed layer which is in background as per the scrolling has been done. Later I bind this function to "onscroll" event of window object so that it does the magic.
I've used lorempixem.com for sample images that you see appearing in background.
Here's the complete code:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Parallax Sample</title>
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
- <style type="text/css">
- .background
- {
- background: url('http://lorempixum.com/1024/768') repeat;
- position: fixed;
- width: 100%;
- height: 300%; /* adjust the height according to the length of contents */
- top: 0;
- left: 0;
- z-index: -1;
- }
- .contents
- {
- color: #fff;
- font-family: arial;
- width: 600px;
- margin: auto;
- line-height: 20px;
- font-size: 16px;
- }
- </style>
- <script type="text/javascript">
- function parallax() {
- var scrolled = $(window).scrollTop();
- $('.background').css('top', -(scrolled * 0.2) + 'px');
- }
- $(window).scroll(function () {
- parallax();
- });
- </script>
- </head>
- <body>
- <div class="background">
- </div>
- <div class="contents">
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- <div>
- <p>
- Sample Image</p>
- <img src="http://lorempixum.com/300/200/sports" />
- </div>
- </div>
- </body>
- </html>
Hope you enjoyed this article. Thanks for reading :)