Troubleshooting the pre-loader image loading problem

Apologies for bringing up a minor issue, but I'm struggling with this one. I added a pre-loader image to my page load using a simple method.

You can find my page here

Here is the HTML code for the Pre-loader image:

 <div class="se-pre-con"></div>

The jQuery script to trigger the pre-loader on page load:

 $(window).load(function() {
      // Animate loader off screen
      $(".se-pre-con").fadeOut();
  });

Some CSS styling used:

 <style>
.no-js #loader { display: none;  }
.js #loader { display: block; position: absolute; left: 100px; top: 0; }
.se-pre-con {
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background: url('http://www.rybena.com.br:8080/RybenaRepository/resource/img/loading.gif') center no-repeat #fff;
}

The Issue: When the page loads, it briefly flashes before displaying the Pre-loader image and then continues loading after the effect is complete. Ideally, the pre-loader should appear before the rest of the page. Can anyone help me figure out what went wrong?

Thank you.

Answer №1

It seems like the HTML structure in the link you shared is not properly formatted:

To ensure that your HTML page follows a structured format, consider something similar to this:

<html>
    <head>
    <script>
    </script>
    </head>
    <body>
        <div class="se-pre-con"></div>
        <div class="site-content"></div>
    </body>
</html>

Inside your script tag, include the following method:

 $(window).load(function() {
    $(".site-content").show(); //or fadeIn (whichever suits your requirements)
    $(".se-pre-con").fadeOut();
 });

Ensure that in your CSS file, you have set the following:

.site-content{     
    display: none;
 }

UPDATE: (Answering your question about calling the loader)

You could create a function like this:

function toggleLoader(show){
     if(show == true){
        $(".se-pre-con").show();
        $(".site-content").fadeOut();
     }
     else{
        $(".site-content").show();
        $(".se-pre-con").fadeOut();

     }
}

Then, on window load event, call: toggleLoader(false),

When making a request, call: toggleLoader(true),

And upon receiving a response, call: toggleLoader(false).

Answer №2

When a page loads, both the content and loader element are visible simultaneously, without any specific loading sequence. However, you can control this by using the following code example. Let's say you have two div elements - one for the pre-loader and another for the main content container.

<body>
     <div class="se-pre-con"></div>
     <div id="container" style="display: none;">
         <!-- Your site's content goes here -->
     </div>
</body>

Add the display: none style to the container div so that the site content doesn't flicker during page load. Then, use the following code snippet to make the container div visible upon page load:

$(window).load(function() {
      // Animate loader off screen
      $("#container").show();
      $(".se-pre-con").fadeOut();
  });

Answer №3

After reviewing your page, I noticed that the code .se-pre-con had a display:none; property added to it. Kindly remove this property as shown below. It should work perfectly after that!

.se-pre-con {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background: url('http://www.rybena.com.br:8080/RybenaRepository/resource/img/loading.gif') center no-repeat #fff;
  display: none;
}

Answer №4

/* Dynamically initiate page Loader */
/* for specific div and entire page */
/*html*/
 <div id="divLoaderContainer">
        <!--Loader for body start-->
        <div class="preLoaderPage nodisplay">
            <img class="loading-image" src="~/Content/Images/preLoader.gif" alt="Loading..." />
        </div>
        <!--Loader for body end-->
        <!--Loader for div start-->
        <div class="preLoaderDiv nodisplay">
            <img class="loading-image" src="~/Content/Images/preLoader.gif" alt="Loading..." />
        </div>
        <!--Loader for div end-->
    </div>
/*css*/
.preLoaderPage {
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: fixed;
    display: block;
    opacity: 0.8;
    background-color: #dae3ec;
    z-index: 99;
}
.preLoaderDiv {
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: absolute;
    display: block;
    opacity: 1;
    background-color: rgba(255, 255, 255, 0.94);
    z-index: 99;
}

.preLoaderPage .loading-image {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 100;
    transform: translate(-50%, -50%);
    width: 120px;
    height: 120px;
    width: 180px;
    height: 180px;
}
.preLoaderDiv .loading-image {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 100;
    transform: translate(-50%, -50%);
    width: 120px;
    height: 120px;
}
/*End of Loader styles*/

/*JS function*/
// for full loader
function showLoader(id) {
    if (id == null || id == undefined || id == '') {
        $("div.preLoaderPage").removeClass('nodisplay');
    }
    else {
        showPartialLoader(id)
    }
}
function hideLoader(id) {
    if (id == null || id == undefined || id == '') {
        $("div.preLoaderPage").addClass('nodisplay');
    }
    else {
        hidePartialLoader(id)
    }
}
// for specific div loader

function showPartialLoader(id) {
    var loaderdiv = $("#divLoaderContainer div.preLoaderDiv").clone();
    $(loaderdiv).removeClass('nodisplay');
    var content = $("div#" + id).closest('div.x_content');
    if (content.length > 0)
        $("div#" + id).closest('div.x_content').append(loaderdiv);
    else {
        $("div#" + id).append(loaderdiv);
    }
}

function hidePartialLoader(id) {
    var content = $("div#" + id).closest('div.x_content');
    if (content.length > 0)
        $("div#" + id).closest('div.x_content').find('div.preLoaderDiv').remove();
    else
        $("div#" + id + ' div.preLoaderDiv').remove();
}


/*Called by JS*/

$.ajax({
           showLoader(id);// id of div or container

            success: function (data) {
               // your code
            },
hideLoader(id)// id of above div or container
        });

Answer №5

.custom-loading-spinner {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background: url('http://www.example.com/loading.gif') center no-repeat #fff;
  display: none;
}

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

Having trouble with my ASP.Net OnClick Subs not functioning as desired

I have implemented onClick handlers to process button clicks that query SQL. The issue I am facing is that these queries sometimes take between 10 to 30 seconds to return a response. To prevent click-stacking during this time, I disabled the buttons. Howev ...

Utilizing SCSS to customize specific dimensions

Currently, I am diving deeper into the world of scss and finding myself a bit perplexed by certain aspects. I'm hoping for some guidance from knowledgeable individuals. One issue I am facing involves defining different font sizes for various headings ...

Making a jQuery Ajax request from a view to a controller method, but not rendering the view that is returned by that method in an MVC

Recently, I encountered an issue with an Ajax call from the Index.cshtml view to the PrintPreview method in the MasterList Controller. I made sure to pass all the necessary parameters for the call. The problem arose when returning the view from the PrintPr ...

Create dynamic modals in ReactJS that appear when a row is clicked

Engaged in a project for an undisclosed entity where patient data is retrieved from their API and displayed as modal on the page. Clicking on a modal reveals more threat information in another modal. The objective is for these modals to render based on a c ...

Looking to add a form within another form using AJAX? Just keep in mind that the initial form should also be inserted using AJAX

I have incorporated a form using AJAX on a Php page. Now, I am trying to add another form within that existing form which is also created using AJAX, but unfortunately, it is not functioning as expected. This serves as the parent page. <div class=" ...

Using JavaScript to develop a demonstration of the capabilities of Microsoft's Cognitive

Having trouble getting this basic example of the Microsoft Cognitive Services to work in JavaScript. Need some help or a working example, please! I've attempted to run the code in both node and browser with necessary modifications. Encountering an e ...

Dynamic Calendar Tool with Integrated Time Picker

Are there any JavaScript and AJAX date picker libraries available that also include the option to select a time range? This would involve choosing a date first, followed by the ability to specify a time range. I'm in search of examples similar to thi ...

Troubleshooting: Select2 Search Functionality Error

After working with the select2 plugin for some time, everything seemed perfect until now. I have a page with 3 selects that load data and function properly, as well as multiple selects inside a popup. The appearance is good, but the search field is not al ...

Crafting an interactive form

Creating a form with 2 options (Yes/No) is my current task. When the user selects one of these options, I want a collapsible subform to appear with specific details related to that selection. If the user chooses the other option, a different collapsible su ...

Media query failing to activate

As I delve into utilizing media queries to adjust background images and hide content within a division tag when the screen size exceeds 640px, I'm encountering issues with the functionality. Being new to this concept, I can't shake off the feelin ...

Adding an object to an array of objects with the useState hook in React - A simple guide

Hello, I am currently working on a project that involves creating a dynamic test page with questions, answers, and points/grades. The goal is to generate input fields based on the number of questions selected by the admin, along with an "Add Question" butt ...

The AJAX POST request is not receiving the JSON data as expected

After creating an HTML form with the following structure: <form id="loginForm" name="loginForm"> <div class="form-group"> <input type="username" class="form-control" id="username" name="username" placeholder="Your username..." > ...

How can I deselect the 'select-all' checkbox when any of the child checkboxes are unchecked?

Here is the code provided where clicking on the select-all checkbox will check or uncheck all child checkboxes. If any child checkbox is deselected, the 'select-all' checkbox should also be unchecked. How can this be achieved? $(document).read ...

When Selenium in JavaScript cannot locate a button element, use `console.log("text")` instead

I am trying to capture the error when there is no button element available by using console.log("No element"). However, my code is not working as expected. const {Builder, By} = require("selenium-webdriver"); let driver = new Builder().forBrowser("chrome ...

Displaying database records in custom fragments using Spring Boot and Thymeleaf

I'm struggling with displaying database records on a webpage in a specific format. To achieve this, I've created a thymeleaf fragment to act as a template for each record in my database. However, I can't figure out how to make these fragment ...

Why is the inner span element not being referenced in jQuery?

I've come across a specific HTML structure: enter code here <ul> <li><span>aaa</span> <div> <ul> <li><span>bbb</span> </li> </ul> ...

What is the best way to extract the src attribute from an image tag nested within innerHtml?

In the developer tools, navigate to console and enter: var x= document.getElementsByClassName('ad-area')[0].innerHTML; x returns: '<a href="/members/spotlight/311"><img class="block-banner" src="https://tes ...

Is there a way to access the route name (path) in Express Node.js while making a request using the req object?

Within my Node.js Express server, I have implemented a generic middleware where I am trying to retrieve the route name. Here is an example: app.use(function (req, res, next) { //using a specific npm package that allows me to execute functions after send ...

Pass an array of files from a Jquery ajax request to a controller action

I am facing an issue where I can successfully pass a single file as System.Web.HttpPostedFileBase, but when attempting to pass an array of files, the controller's action receives null. I have attempted sending an array of files. HTML: <i ...

What could be causing one of my images to not appear on my Gatsby page?

Hey there, I could use some help with this issue: Recently, I created a website using Gatsby.js and deployed it to my web server (which is running NGINX on Ubuntu 20.04). The site uses Gatsby Image for querying and displaying images, and everything seems t ...