How to show a loading gif image while a page loads using javascript and Css

show loading image
Sometimes you want to hide a page from users while it is loading and show an animated gif loading or wait image instead of it. You can check demo.

To download the demo scroll down to bottom.

For this we will add two div id in the css file. One is the #content id that will contain all the content of body inside it. Its display will be set to ‘none’.

Add another div id #loading that will show the loading gif image. Keep its z-index to a high value like 1000 or so.

div#content {
    display: none;
    }

div#loading {
    top: 200 px;
    margin: auto;
    position: absolute;
    z-index: 1000;
    width: 160px;
    height: 24px;
    background: url(loadingimage.gif) no-repeat;
    cursor: wait;
    }

Now add these div id into your html page as follows-

<div id="loading"></div>
<div id="content">your html content here......</div>

Now add the following javascript code into head section of your html page.

<script type="text/javascript">// <![CDATA[
        function preloader(){
            document.getElementById("loading").style.display = "none";
            document.getElementById("content").style.display = "block";
        }//preloader
        window.onload = preloader;
// ]]></script>

Display loading gif demo.

Subscribe to My Newsletter
Receive all new posts and site updates in your mailbox.
Name:
Email:
 
Powered by Optin Form Adder

Tags: , , ,

11 Responses to "How to show a loading gif image while a page loads using javascript and Css"

Leave a Comment