jQuery's ability to load pages seamlessly is unmatched

My website is filled with images, and in order to speed up the loading time, I added a loading screen. Currently, it's a white overlay with the following CSS:

#loader{
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 9999999999999999;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('loader.gif') 50% 50% no-repeat rgb(255,255,255);
} 

Here is the jQuery code I currently have:

$(window).load( function(){

    $("#loader").fadeOut("slow");

});

Right now, the loading screen and the rest of the website load simultaneously, creating a messy look.

Is there a way to ensure that the rest of the page only loads once the loading screen is fully displayed?

Answer №1

After implementing the solution from jQuery callback on image load (even when the image is cached), the code below demonstrates how to handle image loading efficiently:

$(window).load( function(){
    var totalImages = $('img').length, // keep count of total images in the page
        loadedImages = 0; // track images that have loaded

    // listen for the load event on each image just once
    $("img").one("load", function() {
        loadedImages++;  // increment loaded image count
        if (loadedImages == totalImages){
            // all images have loaded, so we can hide the loader
            $("#loader").fadeOut("slow");     
        }
    }).each(function() {
        // check if images have already loaded from cache
        // manually trigger load event for cached images
        if(this.complete) $(this).load();
    });
});

Answer №2

To optimize the loading speed of your website, consider using ajax to load images after the initial page content has loaded. This method can significantly improve the loading time of your website, especially if it contains a large number of images.

Answer №3

Here are some steps to improve your page loading experience:

  1. Start by loading the initial page without showing the loading screen.
  2. Use jQuery to load the loading screen when needed.
  3. Load images dynamically using ajax.
  4. Once the images are successfully loaded, fade out the loading screen.
  5. After that, display the images on the page using the following codes:
#
#loader{
 width: 100%;
 height: 100%;
 position: fixed;
 z-index: 9999999999999999;
 display: none; // initially hidden
 top: 0;
 left: 0;
 right: 0;
 bottom: 0;
 background: url('loader.gif') 50% 50% no-repeat rgb(255,255,255);
}

Jquery:

 $(function(){
     $("#loader").show();

     $.ajax(
      url: "url_to_process_ajax.php",
      type: "GET",
      data: "get=images",  // send image request to server
      success: function(data){
        // handle image processing
        // once done, fade out the loader and show the image area
        $("#loader").fadeOut("slow", function(){
          $("#image_container").show("fast");
        });
      }
     );

});

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 is the best method for transmitting all parameters via a URL?

$(document).ready(function () { $("#submitButton").click(function () { var name = $("#name").val(); var age = $("#age").val(); var gender = $('input:radio[name=gender]:checked').val(); v ...

Ant Design: Incorporating margin and padding causes slight rendering glitches in the markup

https://i.sstatic.net/BWmFU.gif https://i.sstatic.net/ZaLH8.gif When I apply margin and padding to this class, it starts twitching like in the first GIF. Removing the margin and padding fixes the issue, but I need them for styling. What is causing this ...

Tips for including space at the beginning and end of a dynamically created table cell

My table is dynamically generated with contents drawn from a database, creating rows based on the data. Each cell has a rounded border and 2px padding for consistency. I want all cells to appear evenly spaced and padded vertically, but I'm facing an ...

Adding a drop shadow effect to borders using CSS is a great way to

I created this unique button design using Figma and now I want to implement it in CSS. https://i.sstatic.net/b6hNx.png Upon closer inspection, you'll notice that I included a highlight with a white shadow on the border. However, when I try to repli ...

implementing select2 and binding an event to it

I have a simple select2 setup where I am passing a common class in the options and trying to trigger a jQuery event on every change or click of an item in the dropdown. Here is my code snippet: <select name="transferfrom" id="transferfrom" data-placeho ...

Issues with refreshing Datatables functionality

I’ve hit a roadblock while troubleshooting this issue, and it’s gotten quite frustrating. That's why I've turned to Stackoverflow for help once again. As a beginner, I ask for your patience and thank you in advance for any assistance. In my ...

Adjusting jQuery tab content heights to maintain consistent height across tabs and prevent varying heights

I am currently working on a tabbed view that is very similar to the demo found at the following link: http://jsfiddle.net/syahrasi/us8uc/ $(document).ready(function() { $(".tabs-menu a").click(function(event) { event.preventDefault(); $(this).pare ...

Using JQuery in AJAX, you can easily upload numerous images and then handle them using PHP

I need help with something new to me - how can I achieve this using ajax? I know how to do it with plain html multiform parts, but not with ajax. Here is the pseudo code I currently have: HTML: <input type="text" class="imgform" name="imagename" valu ...

Why does my text keep aligning to the left when I adjust the width, causing my background to remain attached to the text?

When I add a width attribute to my #information h1 id, it seems to override the text-align: center; property and aligns the text/background to the left instead of center. Basically, I need assistance in centering the headers with the appropriate backgroun ...

Methods for concealing the title and date when printing web content using JavaScript

When utilizing the window.print method to print out a specific screen, I encountered an issue. I need to hide the date in the top left corner of the image as well as the title (not the big heading) which has been intentionally blurred. I've come acro ...

Issues with the animation of the navbar menu button are preventing it from functioning

I have been attempting to incorporate animation when the navbar-button is toggled on smaller screen sizes. Inspired by the design of .navbar-toggle.larr in this particular template, I tried to implement a similar effect. /* ANIMATED LEFT ARROW */ .navbar- ...

Using jQuery to reference my custom attribute---"How to Use jQuery to reference My

Can you explain how to reference a tag using a custom attribute in jQuery? For example, if I have a tag like this: <a user="kasun" href="#" id="id1">Show More...</a> I want to reference the tag without using the id. So instead of using: $( ...

Introducing a new div element completely disrupts the overall layout

Take a look at the code I have provided below. http://jsfiddle.net/xymgwonu/3/ Everything was working perfectly fine until I introduced a new div with the class name <div class="bubble"></div>. This caused some issues with the overall design. ...

Using jQuery to locate the href attribute within a TD element with a specific

Update URL <td class="posts column-posts"><a href="edit.php?tshowcase-categories=ops&amp;post_type=tshowcase">2</a></td> Current URL: <span class="view"><a href="https://blog.company.com/team/tshowcase-categories/ops ...

When a td element is clicked, the Textbox will also be clicked

Similar Question: Dealing with jQuery on() when clicking a div but not its child $(oTrPlanning).prev().children('td').each(function () { this.onclick = setCountClick; }); When a TD element is clicked, the function setCountClick() i ...

What could be causing my scrollable div to not function properly within a Bootstrap modal?

I am facing a frustrating issue. I have a simple DIV element that I want to make scrollable, containing a table inside it. The process should be straightforward as I have a separate .html file with the code that functions correctly. Here is an excerpt of ...

Combining a serialized date with an array for an Ajax post: What you need to know

Is there a way to combine a serialized date with an array using jQuery for AJAX post requests? For example, var array = { name: "John", time: "2pm" }; $.post( "page.php", $("form").serialize() + array); Currently, the output is: Array ( [url] => ...

"Encountering an issue with AJAX file upload displaying an error message for

Before I showcase my code, allow me to explain my objective. My goal is to create a page that updates a user's details in the database using AJAX. Initially, I successfully achieved this task. Subsequently, I wanted to enhance the functionality by inc ...

Show a small section of a webpage within the same webpage in MVC 4

Within my application, I have utilized the div.load() method in jQuery to load a partial view within a common View. I am currently attempting to update the values within the partial view and display it in the same view using jQuery. Are there any other met ...

Guide to integrating promises into the HTML5 Geolocation API

How can I fix the code below to ensure that the getPreciseLocation function does not return an undefined value? In essence, when the user clicks on the #precise-location-prompt and shares their location with the browser, there should be an AJAX call to re ...