Floating div on Page Scroll Using JavaScript

Introduction

This article explains how to create a div that will float up and down when the page is scrolled.

You must have seen in many websites that some advertisements always float up and down whenever you scroll the page, you can also create containers such as that for your website that will hold the Advertisements and will help you to earn more.

If your web page has only one div then it is very easy to create such type of Advertisement Container by just making it's Position=Fixed but what to do when you are using more than one Div on the same page and you want your advertisement to work on the complete page, in other words on all divs? In that case you can refer to this article since it will help you to create such type of Div Containers that will work on the complete page and not on the single Div.

Now for the procedure to create such types of containers.

Step 1

First of all add a webpage to your Visual Studio application.

float div1.jpg

Now you need to add three divs to your Web Form, one of them will be applied to the complete web page and will work as parent div and the other two will be children of this parent div.

<body>

    <div id="page-wrap">

        <div class="div" id="Div1">

        </div>

        <div class="div" id="Div2">

        </div>

    </div>

</body>

You can use these two children divs for creating a form or displaying the information according to your site's requirements.

Step 2

Now you need to add one more div that will be the Container Div where advertisements are to be placed.

<body>

    <div id="page-wrap">

        <div class="div" id="Div1">

        </div>

        <div class="div" id="Div2">

        </div>

    </div>

    <div id="floatDiv" class="child_div">

        <label style="color:black; font-size:14px;">Advertisement</label> "

    </div>

</body>

Here "Float Div" is the Advertisement Div.

Step 3

Now you need to add some jQuery and some JavaScript to your web page head section.

You need to add two jQuery files to your application, in other words "jquery-1.3.2.min.js" and "jquery.dimensions.js". You can find these files in the Zip File that I provided above. Provide the reference of these files in the head section of your application.

After passing the reference of jQuery files you need to add some JavaScript to your application that will help the Advertisement Div to float.

    <script src="jquery-1.3.2.min.js"></script>

    <script src="jquery.dimensions.js"></script>

    <script>

        var name = "#floatDiv";

        var menuYloc = null;

        $(document).ready(function () {

            menuYloc = parseInt($(name).css("top").substring(0, $(name).css("top").indexOf("px")))

            $(window).scroll(function () {

                offset = menuYloc + $(document).scrollTop() + "px";

                $(name).animate({ top: offset }, { duration: 500, queue: false });

            });

        });

        </script>

Step 4

Now you just need to add some style to this application so that everything can be seen properly.

   <style type="text/css">

        body {

            color: #ccc;

            font: 10px "Lucida Grande", "Lucida Sans", "Trebuchet MS", verdana, sans-serif;

        }

 

        #Div2 {

            height: 800px;

            width: 1600px;

            border: 1px solid black;

        }

        .div {

        }

 

        .child_div {

        }

        #Div1 {

            height: 800px;

            width: 1600px;

            border: 1px solid black;

        }

        #floatDiv {

            position: absolute;

            top: 150px;

            left: 50%;

            margin-left: 235px;

            width: 100px;

            height: 250px;

            border: 1px solid black;

        }

    </style>

Now your application is ready to run so debug your application and check the output.

Output

When you debug your application you will get output like this:

float div2.jpg

 

Now when you scroll down you will see that your Advertisement Div is automatically floating down with you.

Div Animation.gif

Up Next
    Ebook Download
    View all
    Learn
    View all