Basics of creating Parallax Effect using HTML CSS and JavaScript

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

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 for sample images that you see appearing in background.

Here's the complete code:

Parallax Sample

background: url('') repeat;
position: fixed;
width: 100%;
height: 300%; /* adjust the height according to the length of contents */
z-index: -1;
color: #fff;
font-family: arial;
width: 600px;
margin: auto;
line-height: 20px;
font-size: 16px;

function parallax(){
var scrolled = $(window).scrollTop();


Drag Drop & MouseMove Event Handling in Javascript

Below is an example of mousemove event handling that has mostly been used with drag & drop. Three DIVs are used to demonstrate in this example, the first one contains a picture on which I’ve performed Drag & Drop to move it’s container. The second one records all the mouse movements with it’s current location and deviation on each single move. The third one is used here to demonstrate a little animation using javascript. Even it can be done with the only javascript, I’ve attached JQuery to make the div run smooth. If you click anywhere in the document the circle object will follow the cursor and if you click and drag the picture object it will move with the cursor. You can also find a live demo link at the bottom of this post for the same.

(Note:Please report any broken link)
Drag & Drap Demo

border-radius:15px 15px 15px 15px;
background:url('') no-repeat;
box-shadow:0 0 15px 5px red, 0 0 10px 5px silver inset;
-webkit-user-select: none; 
-moz-user-select: none; 
    box-shadow:0 0 15px 5px green, 0 0 10px 5px silver inset;
    box-shadow:0 0 15px 5px blue, 0 0 20px 10px #dedded inset;

    var currentTop;
    var currentLeft;
    var dragObject;
    function mouseUp() {
        dragObject = null;
    function makeDraggable(obj) {
        obj = document.getElementById(obj);
        obj.onmousedown = function () {
            dragObject = this;
            currentTop = obj.offsetTop;            
            currentLeft = obj.offsetLeft;
    function mouseMove(event) {
        if (dragObject) {
            this.onselectstart = function () { return false; }
            var txtDiv = document.getElementById("textDiv");
            var theEvent = event ? event : window.event;
            var x = theEvent.clientX;
            var y = theEvent.clientY;
            var moveX = x - currentLeft;
            var moveY = y - currentTop;
            var adjustX = dragObject.offsetWidth / 2;
            var adjustY = dragObject.offsetHeight / 2;
            $(dragObject).animate({ left: currentLeft + moveX - adjustX, top: currentTop + moveY - adjustY }, 1);           
            txtDiv.innerHTML += "Current Position: Top:-"+currentTop+" Left:-"+currentLeft+"
            txtDiv.innerHTML += "Mouse Moved: X: "+moveX+"  |  Y: "+moveY+"
            txtDiv.scrollTop = txtDiv.scrollHeight;
    document.onmousemove = mouseMove;
    document.onmouseup = mouseUp;
    document.onmousedown = mouseDown;
    function mouseDown(event) {
        var div = document.getElementById("testDiv");       
        $(div).animate({ top: event.clientY - 50, left: event.clientX - 50 }, 1000, function () { = get_random_color(); });        
    function get_random_color() {
        function c() {
            return Math.floor(Math.random() * 256).toString(16)
        return "#" + c() + c() + c();
    window.onload = function () {



A working demo is here: DragDropDemo