Top-notch loading icon using jquery

Recently, I embarked on the journey of learning Javascript and jQuery simultaneously, and to my surprise, it feels quite manageable.

As I delve deeper into creating functions, I am currently working on displaying a loading image. While I initially used one from , I now desire something more dynamic - perhaps an image that changes with each load...

On another note, there is an issue where my loading image sometimes appears beneath other elements without any apparent reason. I am determined to unravel this mystery and make sure it doesn't happen again. Any insights or tips would be greatly appreciated!

Below you can see my current setup:

...
<img id="loading" src="loading.gif" />
...
<script>
function load(){
 $("#loading").fadeToggle();
}
</script>

Answer №1

I found this concept very intriguing and have implemented it in my own projects! Here's how I went about doing it:

Feel free to customize this to suit your preferences, as there are plenty of ways to enhance the aesthetics. However, here is a simple configuration you can start with.

CSS :

.preloader{
    z-index: 1001;  // The higher the value, the more it will be displayed on top. I chose 1001 because I typically don't exceed z-indexes of 1000, but it may vary for you.
}

HTML :

<div class="preloader">
    <img src="images/preloaders/1.png" /> <!-- You can customize the size of the preloader image and apply styles to cover the entire page for a 'modal' effect -->
</div>

Javascript (jQuery) :

<script>
var MAX_PRELOADERS = 5; // How many unique preloaders do you have? Ensure they are named appropriately: 1.png , 2.png ... 5.png (in the correct folder)

function showHidePreloader(){ 
    $(".preloader img").fadeToggle();
}

function changePreloader(){
    var new_preloader = Math.round(Math.random() * MAX_PRELOADERS) + 1; 
    $(".preloader img").attr("src","images/preloaders/"+new_preloader+".png"); // Adjust the path and extension if necessary
}
</script>

By the way, I use this site for preloaders: I highly recommend checking it out as it offers superior options compared to what you currently have. Simply call the changePreloader() function when you want to switch preloaders, and you already know how to use the first function :)

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

What could be causing the misalignment between the desired output and the response from the AJAX request

Below is a basic JavaScript file I am working with: $.ajax({ url: "is_complete.php", type: "post", success: function (data) { if(data == 1) { } alert("ok") } }) The message "ok" will only be di ...

Display subpages of WordPress site in a dropdown menu on the navigation bar

Currently in the process of converting an HTML website to Wordpress, I am encountering a challenge. The issue lies in my attempt to add a drop-down feature to the navigation list using the wp_list_pages tag. As it stands, when hovering over the "products" ...

validating jQuery on multiple form fields

When dealing with validation in jQuery on multiple fields where the form field names/ids vary for each form, what is the most effective approach? For example: <form> <p>Select Colour</p> <select name="M100" id="M100"> ...

Form-box powered by jQuery Ajax

Looking for a solution to avoid loading a new page when adding items to a list of several items. How can I bind in a window/box? Considered using the jQuery plugin "jQuery-ui-dialog," but unsure if this is the right approach. Also, facing challenges with ...

Challenges of implementing an infinite scroll feature using PHP, jQuery, MySQL, and AJAX

Hey there! I'm in the process of developing this website: It's almost complete, but I'm facing a challenge with the "Coleccion" section. In this section, users select a specific type of bag, and only 20 bags are initially loaded from a MySQ ...

Tips for creating a dynamic variable for an object key in jQuery to streamline your code

Utilizing the TweenMax library, I am adding styles to a div while scrolling. However, I am facing a challenge where I need different styles based on varying page resolutions. To address this issue, I implemented an if condition. Is there a way I can use a ...

Using PHP variables in HTML frameset URL for dynamic content passing

Looking for help to pass a PHP variable through a frameset URL. I've been trying different techniques without success so far. Can anyone provide some guidance? Here is an example of my code and what I have attempted: <?php $var = 2+3 ?> < ...

Generate a PDF document on the user's device

Is there a way for me to trigger the print command on a PDF file without relying on Adobe PDF viewer's print button? I'm interested in using a separate event instead of the print button within the PDF viewer. Is this achievable? ...

The execution of jQuery was hindered due to the implementation of PHP include

Having an issue with jQuery not working in index.php when including the file header.php. The nav sidebar is included, but clicking the chevron does not trigger anything. It only works when I directly include header_user.php without checking the UserType in ...

Performing automatic submission of form data without needing to redirect or refresh the page using Javascript

I am trying to find a way to automatically submit form post data without the page redirecting, but I haven't had success with any of the examples I've found that involve jquery and ajax. Currently, my code redirects the page: <!DOCTYPE html& ...

Executing a parent window function from a child window using AngularJS

I'm working on a multi-page/window application where a dashboard page controls a list of children (windows). The dashboard should be able to call functions in the children and vice versa. However, I'm facing an issue where the child fails to invo ...

The settings of the button return to their default state once it is clicked on

I've been working on a small project and I added a button with hover and CSS effects. However, the issue is that once the button is clicked, it reverts back to its basic state without any of the CSS properties applied. I attempted to troubleshoot if ...

When a jQuery accordion panel is opened or closed, trigger additional code to run

Is there a way to trigger additional JavaScript code to run after the panel in the jq accordion is opened and closed? I couldn't find any relevant events documented. ...

Create a one-page website that utilizes the jQuery load() function

I'm interested in creating a single-page website with top navigation, left sidebar, and a #content div for content exchange. $(document).ready(function() { // or click on a button $('#content').load('somepage.php'); }); <scrip ...

Aligning three hyperlinks evenly to spread across the webpage

My professor provided the class with an image that may resemble something he could ask us to recreate on an upcoming exam. He suggested we try replicating it at home to study. I have most of it figured out, but there are three links positioned perfectly ac ...

Incorporating Dynamic Javascript: A Step-by-Step Guide

I came across a select object that looks like this: <select id="mySelect" onchange = "start()" > <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option> < ...

Using the HTML <span> and svg elements together does not allow them to be inline with each other

In my current scenario, I have a fieldset that is inline with another one on the page. The specific fieldset in question contains an SVG circle and some text. My objective is to have both elements inline with each other, while allowing the fieldset to adj ...

Enhance the appearance of your image by adding a stylish frame or border without altering its

Is there a way to add a frame to an image without causing it to shrink due to borders? Below is the code I have tried using: .img-border { height: 100%; width: 100%; position: absolute; z-index: 1; } <div class="border border-black i ...

Show 1 Blog Post on a Separate AngularJS Page

I would like to show only Test Post 1 on the next HTML page titleDetails.html when the user clicks on Test Post 1 in index.html 1) titleDetails() in index.html: <a ng-click="titleDetails(post)">{{ post.title }} </a> 2) Controller Variables a ...