Displaying an image once half of the page has been loaded

My intention is to display a .gif image while the page is loading, however, currently the .gif image only appears after half of the page has loaded.

I would like the .gif image to be displayed before the actual page content loads, or at least as soon as the page layout starts to show.

.HTML code

.Head

<head>
<style type="text/css">
    div.loading
    {
        filter: Alpha(Opacity=40);
        -moz-opacity: 0.7;
        opacity: 0.7;
        width: 100%;
        height: 100%;
        background-color: #ffffff;
        position: fixed;
        z-index: 500;
        top: 0px;
        left: 0px;
        display: none;
    }       
</style>

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        // executes when HTML-Document is loaded and DOM is ready
        $('#loading').fadeIn();
        $("#index").load("index.aspx");

    });
    $(window).load(function () {
        $('#loading').fadeOut();

    });
</script>

.body

<body>
<div id="loading" class="loading">
    <img src="data:image/gif;base64,R0lGODlhEAAQAPQAAP///wAAAPDw8IqKiuDg4EZGRnp6egAAAFhYWCQkJKysrL6+vhQUFJycnAQEBDY2NmhoaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAAFdyAgAgIJIeWoAkRCCMdBkKtIHIngyMKsErPBYbADpkSCwhDmQCBethRB6Vj4kFCkQPG4IlWDgrNRIwnO4UKBXDufzQvDMaoSDBgFb886MiQadgNABAokfCwzBA8LCg0Egl8jAggGAA1kBIA1BAYzlyILczULC...
        alt="Loading" class="imggif" align="middle" style="position: absolute; top:40%; left:50%; z-index: auto;" />
</div>
</body>

Answer №1

The fadein function will use the default time of 400ms for the animation to appear. It seems like this is causing your image to load after half the page has already loaded. You can try using the following code:

$('#loading').show();,
$('#loading').hide()

or

 $('#loading').fadeIn('fast');

For a better understanding, please refer to the following link: http://api.jquery.com/fadeIn/

Try using the body onload function once.

<body onload="fnload();">
<div id="loading" class="loading">
    <img src="image\loading.gif"
        alt="Loading" class="imggif" align="middle" style="position: absolute; top:40%; left:50%; z-index: auto;" />
</div>
</body>

The function fnload() should look like this:

function fnload() {
    $('#loading').fadeIn('fast');
}

Answer №2

Give this a try, it should be effective for you .html code

<head>
<style type="text/css>
div.loading
{
    filter: Alpha(Opacity=40);
    -moz-opacity: 0.7;
    opacity: 0.7;
    width: 100%;
    height: 100%;
    background-color: #ffffff;
    position: fixed;
    z-index: 500;
    top: 0px;
    left: 0px;
    display: none; // Make sure to remove this line from your css
}       
</style>   

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

<script type="text/javascript">
// No need to include $(document).ready(function ()});

$(window).load(function () {
    $('#loading').fadeOut();

});
</script>

.body

<body>
<div id="loading" class="loading">
<img src="data:image/gif;base64,R0lGODlhEAAQAPQAAP///wAAAPDw8IqKiuDg4EZGRnp6egAAAFhYWCQkJKysrL6+vhQUFJycnAQEBDY2NmhoaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQABAAAAV2ICACAmlAZTmOREEIyUEQjLKKxPHADhEvqxlgcGgkGI1DYSVAIAWMx+lwSKkICJ0QsHi9RgKBwnVTiRQQgwF4I4UFDQQEwi6/3YSGWRRmjhEETAJfIgMFCnAKM0KDV4EEEAQLiF18TAY...

This solution is guaranteed to work for you

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I do not send JSON Express post for receiving

I have a question regarding sending JSON data to my Express server using request. However, when Express receives the data, it doesn't seem to be in the correct JSON format. Below, I will share the code related to this issue. The JSON I am sending ...

What should I do with this massive 800kb JavaScript file?

After learning that concatenating and possibly gzipping a javascript file could enhance the speed of an application, I decided to test it out. It seemed like most of my pages (although not all) were downloading around 1mb of javascript, so why not compres ...

Creating a seamless database structure for shopping carts and efficiently organizing the order placement process within the tables

In the process of developing a database within SQL Server 2005 to manage order data. The [customers] table: This stores customer details with the primary key being customer_ID, which autoincrements. The [orders] table: Consists of 3 columns - [orderid] ( ...

Is Angular JS binding case-sensitive?

Currently, I have a dropdown menu displaying a list of countries from a JSON file. Beside it, there is a text field that correlates to the dropdown, showing the ISO 3166 alpha-2 data (e.g. CH for Switzerland). Users have the option to input the 2-characte ...

Restrict Recursive GraphQL Schema Query Depth with graphql-sequelize resolver in Node.js using express-graphql

In my code, I have two models called User and Post. My goal is to retrieve User information when querying a post, as well as obtain all of a User's posts when querying a user. These two models are associated in the following manner: User.hasMany(Pos ...

Calculate the position of an element's top property using dynamic JavaScript calculations

I am currently working on a script to control the scrolling speed of elements on a webpage relative to their parent div. The code I have written so far is as follows: $('#two').css({'top' : 600-($(this).scrollTop() / 1.2)+"px"}); The ...

Is it possible to increase Google+ shares without needing an API key for every single page?

Looking to retrieve the number of Google+ shares for each URL on a search results page using Ajax. Facebook and Twitter share counts have been successfully implemented: $.getJSON('http://urls.api.twitter.com/1/urls/count.json?url=' + url + & ...

Calendar: Display upcoming dates within the next week starting from the current week

Hey there! I have a calendar that includes next and previous buttons. When the user clicks on the next button, the schedule for the upcoming week will be displayed. However, if the user clicks again, nothing happens. My goal is to only show dates for the n ...

Having trouble loading the image source using JSON in Vue.js

var topArticle=new Vue({ el:'#toparticle', data:{topmostArticle:null}, created: function(){ fetch('topnews.json') .then(r=>r.json()) .then(res=>{this.topmostArticle=$.grep(res,functi ...

Struggling to find the accurate height of an element in React

Trying to display text in two rows using a React component. However, I am facing issues with getting the updated height of the element - it only shows the height after rendering. Below is my current component: constructor (props) { super(props) ...

The Jest mock is infiltrating the function logic instead of simply returning a rejected promise

The Issue at Hand I am currently working on testing the correct handling of errors when a use case function returns a rejected promise along with the appropriate status code. However, I seem to be encountering an issue where instead of getting a rejected ...

Having trouble with React Jest testing for onSubmit using simulate?

My first week dealing with testing has left me feeling a bit confused. I'm currently working on testing the SignIn component. Initially, I tested the snapshot to ensure that the mockup behavior remains unchanged. Now, my focus is on testing the submit ...

What is the best way to store query responses in global.arrays without overwriting the existing values stored within the array elements of global.arrays?

QUESTION: I am struggling to efficiently assign results of MongoDB queries to global arrays. I attempted to store references to the global arrays in an array so that I could easily assign query results to all of them using a for loop. However, this appro ...

Is there a way to stop the page from scrolling once I reach the bottom of a specific div within it?

My webpage has multiple divs that share a similar structure: <div style="height:200px; overflow:auto;"> <div style="height:300px;"> <!--There is a lot of content here--> </div> </div> When the user hovers ove ...

Notifying users of errors on the server using Node.js router

One of the features on my website allows users to change their passwords. However, a problem arises when an error occurs on the server during the password change process. In such cases, I have to display a warning message like: 'Warning, the passwor ...

Scrolling through a Telerik MVC Grid with dynamic content

I'm currently working on removing pagination from a couple of Telerik MVC Grids that are loaded with a significant amount of data (over 5000, potentially more upon production). For smaller grids, removing the Pageable property usually does the trick a ...

How can local JavaScript be executed once remote JavaScript is loaded?

On my webpage, I am using jquery/ajax to fetch a section of HTML/JS code from another component and inserting it into the page. This HTML includes references to additional JS files that need to be loaded before running my own javascript. The injected HTML ...

Ways to display a different landing page when navigating to the homepage of a website

In the Next application, I have set up a dynamic route at the root of my pages folder as src/pages/[page].js While this works smoothly for pages with slugs like example.com/my-page, it poses a challenge when trying to access a designated slug named homepa ...

Problems arise when using AngularJS' .run function after navigating to a different page

I have encountered an issue with ngRoute while navigating between pages in my web application. The main login page is called index.html, and the routing is controlled by the main js file. However, I face a problem when trying to use a .run block on a speci ...

Can a function be returned using AJAX?

Suppose I am in need of initiating an ajax call to my server $.ajax({ type: 'POST', url: 'url/url', success: function(response){} }); and upon receiving a response from the server, I send some JavaScript code res.send(&qu ...