How can we use JavaScript to implement a loading progress bar for a CSS div?

Once the CSS div loads, it will replace the progress bar that is currently displayed on the screen.

The CSS div is dynamic and takes some time to fetch the necessary data for display. To indicate this loading process, a Loading bar is shown before the CSS div is loaded completely.

<div id="'+levelDivId+'" class="mainContainerLevel" style="display:none">

<div class="nav-header-top">

// code responsible for fetching data

</div>

A progress bar is shown first before displaying the content inside the following div:

<div id="'+levelDivId+'" class="mainContainerLevel"> </div>

The progress bar will be visible for 2-3 seconds before the dynamic div is displayed.

To accomplish this functionality, a piece of javascript is needed.

Any assistance provided would be greatly appreciated.

Solution Found, here's how:

We have a mainContainer which acts as a container for all elements.

Start by adding the "loading" div to show the loading state:

$("#mainContainer").append("<div id='loading_MainMenu' class='mainContainerl' style='display: block;'><br>
<p>Loading Data....</p> </div>");

Next, include the Data div which needs time to load beneath this comment:

Hide the "loading" div once the Data div is ready to be displayed:

$("#loading_MainMenu").hide();

Finally, append the Data div to the mainContainer like so:

$("#mainContainer #"+DataDiv).append("</div> </div>");

That concludes the procedure.

Answer №1

If you want to initially hide a div, you can set its display property to none at the beginning. Once your data is loaded, you can remove the hidden CSS class using jQuery's removeClass() method. Here is an example:

<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  </head>
  <style>
    .hide{
      display:none;
    }
  </style>

  <div id="example" class="hide">
  </div>
  
  <script>
    $(document).ready(function(){
        //trigger a custom event, change 'testEvent' to an appropriate name
        $("#example").on('testEvent', function(){
            $("#example").removeClass('hide');
        });
    });
  </script>

</html>

After implementing this code, make sure to modify your question so that others can easily understand the issue you are facing.

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

An in-depth overview of the express module export, its internal composition, and the distinct characteristics distinguishing the app object from the express object

Currently, I am delving into learning expressjs, but somehow ended up perplexing myself. It would be greatly appreciated if someone could provide some clarification. My main confusion lies in how the exported express module can function as both a function ...

If the JSON value meets the specified condition, the radio button will be selected for display

Displaying Json data: The following JSON data can be altered based on conditions: {"1":"2","2":"2"} I am attempting to set the radio button as checked if the JSON contains a value that matches with the radio button. The first value in the JSON represents ...

What is the best way to show a block of text when hovering over an image with a smooth easing effect

How can I make a block of text appear with ease when hovering over an image of a tree? The current setup works, but the transition effect is not as smooth as I want it to be: HTML: <p id="hover-text1" class="hover-text1"> Accomplished! </p> & ...

Enjoy the sleek Bootstrap 4.1.0 carousel featuring Font Awesome controls, but keep in mind that the controls will vanish when

I created a Bootstrap 4.1.0 carousel with Font Awesome controls positioned outside the images using CSS. However, when hovering over the control arrows, they disappear. Here is a link to the codepen I created: https://codepen.io/mlegg10/pen/qYEegR Co ...

Are you in need of a BISON middleware solution for socket.io encoding and decoding?

Is there a method to manipulate the data transmitted by socket.io just before it is sent or received? I was considering implementing something like an express middleware. This way, I could encode the data after the normal .emit() call and before the socket ...

having trouble with developing a dropdown menu using jquery

I'm currently creating a drop-down menu for my website and here is the code I'm using: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html dir="ltr"> <head> <met ...

Tips for incorporating personalized CSS properties, such as font-size, into all Django admin pages

Currently, I am in the process of developing a project using Django. My goal is to implement custom CSS properties, such as font-size, across all Django admin pages - including User change and User add pages - simultaneously. This would streamline the proc ...

How can you create an array in JavaScript?

Learning JavaScript has led me to discover the various methods for declaring arrays. var myArray = new Array() var myArray = new Array(3) var myArray = ["apples", "bananas", "oranges"] var myArray = [3] What sets them apart and which ways are typically ...

Executing code after rendering a partial in Rails 4

I am facing an issue with my js.erb file when it is called via AJAX through a controller action. The js.erb file renders a partial that contains a data attribute generated from the model. I need to find a way to execute specific code only after the partial ...

How can I suggest the return type of a function that is out of my control?

When I attempt to parse a JSON-formatted string, a linter error is triggered: let mqttMessage = JSON.parse(message.toString()) // ESLint: Unsafe assignment of an `any` value. (@typescript-eslint/no-unsafe-assignment) Given that I am in control of the con ...

What is the best way to use ajax to send a specific input value to a database from a pool of multiple input values

Welcome everyone! I'm diving into the world of creating a simple inventory ordering site, but am facing a roadblock with a particular issue: Imagine you have a certain number (n) of items in your inventory. Based on this number, I want to run a &apos ...

Jumbotron text alignment in Bootstrap 3

I created a webpage using Bootstrap 3 that looks perfect on a computer screen. However, when viewed on a mobile screen, the text in the jumbotron is overflowing, causing a horizontal scroll bar to appear. <div id="home"> <div id="jumb" clas ...

Tips on dynamically changing the position of a div: Utilize absolute positioning within a for loop

Is there a way to extract the position x value of my div and store it in a variable? How do we then iterate over it within a for loop? <div id="object"></div> <style> #object{ position:absolute; width:10px; height:10px; ...

What is the method for deserializing the JSON response in Swift?

Having received a json response, I am uncertain about how to deserialize it. My aim is to retrieve the Id and Name fields from the following json response and save them as key value pairs in a dictionary. JSON Response ("[{\"attributes\":{&bsol ...

Currently utilizing Yii framework to customize the appearance of a DIV element by specifying the CSS properties within the View, and subsequently storing the CSS file

I am looking for a way to allow users to customize the properties of a parent div, such as color, background, and other CSS styles, directly from the WordPress theming dashboard interface. Can someone please guide me in the right direction? ...

After running the command "npx/npm create-react-app hello" to create a react app, I received the following message

Whenever I try to execute this command for creating a React app: C:\WINDOWS\system32> npm i create-react-app -g hello I receive the following message in my cmd prompt: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf ...

When the document exists, the findOne() method returns null

Running locally, but accessing an atlas MongoDb DB online, I have builder's data like this: [ { "_id": "5ec63e97516541c07c2b26d3", "name": "Bob" }, { "_id": "5ec64261b9be7b08cb8d7ba9", "name": "builder post test" } ] Although s ...

Selenium extracts network information by utilizing the HAR Export xpi plugin

Currently, I am using HAR Export XPI to capture the network traffic data of the pages being accessed via Selenium. To achieve this, I have added the XPI extension to Firefox version 46 since it is not compatible with newer versions. The profile settings I ...

Utilizing the power of WebGL and three.js, merge an alpha-transparent texture with an alpha-transparent color using a fragment

I'm facing an issue with a fragment shader where I have a texture being passed to it and I want to overlay a color with 50% alpha on top of it. While I've managed to partially achieve this, the original texture seems to lose its alpha channel. H ...

The attempt to install myapp using npx create-react-app has failed. The installation has been aborted

Attempting to create a new project using npx create-react-app my-app resulted in an error message saying Aborting installation. https://i.sstatic.net/IhpQJ.jpg Initially, I had node v14.17.1 installed. Upgrading to the latest version 16.4.0 did not resol ...