The preloader vanishes prematurely before the completion of the HTML and Javascript loading process

Hey there,

I'm currently working on implementing a page preloader following the tutorial provided in the link. However, I seem to be encountering an issue with the JavaScript portion as my preloader disappears quickly while the rest of the page continues loading.

I suspect that the presence of the "no-js" class in the HTML might be causing this problem.

Here's the tutorial I've been using

<!doctype html>
<html class="no-js animated fadeIn" lang="EN">
<head>
</head>
<body>
<div class="loader hidden">
<img src="../mirror/img/logo/LOGO_Loadscreen.gif" alt="loading logo">
</div>
    <header>
    </header>
    <main class="black-background">
    </main>

    <script src="js/vendor/modernizr-3.5.0.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-3.2.1.min.js"><\/script>')</script>
    <script src="js/plugins.js"></script>
    <script src="js/main.js"></script>
    <script>
        document.documentElement.className = 
        document.documentElement.className.replace("no-js","js");
    </script>

    <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
    <script>
        window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;
        ga('create','UA-XXXXX-Y','auto');ga('send','pageview')
    </script>
    <script src="https://www.google-analytics.com/analytics.js" async defer></script>
            <script type="text/javascript">
        window.addEventListener("load", function () {
            const loader = document.querySelector(".loader");
            console.log(loader);
            loader.className += " hidden"; // class "loader hidden"
        });
    </script>
</body>

.loader {
position: fixed;
z-index: 12000;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: var(--light-color);
display: flex;
justify-content: center;
align-items: center;
}

.loader > img {
width: 80px;
}

.loader.hidden {
animation: fadeOut 1s;
animation-fill-mode: forwards;

}

@keyframes fadeOut {
 100% {
    opacity: 0;
    visibility: hidden;
 }
 }

Answer №1

The loader div already has the class hidden applied to it.

To fix this, change

<div class="loader hidden">
to <div class="loader">.

Keep the javascript code unchanged and when the "load" event listener is triggered, the hidden class will be added causing the loader to fade out.

Right now, the image is displayed until the css loads, at which point the hidden class takes effect. This explains why the results occur as expected but not at the right time.

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

javascript highlight text within quotes

By incorporating a handy plugin into my application, users can now easily search for specific text and have it highlighted: Check out the Johann Burkard Text Highlighting Plugin here! I've managed to make this work for highlighting a single text str ...

Restricting the user to input only once, using MeteorJs framework

I have been developing an application that allows users to book courses, and I am looking to restrict them to booking each course only once. Below is the current code snippet that I have written for this functionality, and any assistance from you all would ...

Is there a way to automatically close a created div by clicking anywhere outside of it?

I'm facing an issue with a dynamically created div that I want to close by clicking outside of it. However, when I try to achieve this functionality, the div closes immediately as soon as it is opened. closediv(); } function closediv() { d ...

Shifting around haphazardly positioned crates using the cursor

I've successfully developed a program that fills the canvas with randomly positioned boxes of various colors. While this task was relatively easy, I'm now facing a challenge in moving these colored boxes around using my mouse. Additionally, I wan ...

The div remains unchanged when the button is clicked

My webpage is filled with several large div elements. There's a button on the page that, when clicked, should switch to the next div. Despite my efforts, the code I've written doesn't seem to be working as expected. :( This is the HTML st ...

I'm looking to replicate the testimonial slider from the link provided. How can I achieve this?

I'm looking to replicate the testimonial section layout seen on Kanbanize, which features both vertical and horizontal sliders. I'm unsure of how to link the two sliders so they move in sync. I typically use Swiper for carousel/slider functionali ...

Issue with font icons not displaying properly on Firefox

In Firefox, the icon is not displaying in the center, but it works fine in Opera. body { height: 100vh; display: grid; place-items: center; } .link { background-color: red; padding: 3px; display: grid; place-items: center; ...

Tips for creating animations with multiple elements that share the same classes

I am trying to create expandable boxes that can open onclick and close again by clicking on the X. The issue I'm facing is that the close jQuery function isn't working. However, my main concern is how to optimize the code so it doesn't becom ...

Exploring dynamic JSON traversal with jQuery

After receiving a JSON response from the server, I encountered an issue: {'error':'true',fields:[[{'pk':2,'title':'test'}],{'votes':20,'cant':{1:0,2:3}}]} Console Dev return Object { er ...

Modifying the parent element's property when at least one child is selected: A simple guide

I am working with a tree structure made up of nodes called LayerNode. Each node has references to its children and parent. My goal is to update the property of a parent node if any of its child nodes have the property selected = true. public recursiveSe ...

What is the best way to stop an embedded mp4 video from playing when a dynamically generated button is clicked?

After making modifications to the QuickQuiz code found at https://github.com/UrbanInstitute/quick-quiz, I have replaced the img with an embedded mp4 video that loads from a JSON file. Additionally, I switched from using the original SweetAlert library to S ...

Is it possible to view the object sent from AJAX to PHP in PHP using a debugger?

I'm facing an issue where I am making an AJAX call to a PHP file, sending a JSON object as a parameter from JavaScript. The PHP file is supposed to perform some logic with the object and return it using json_encode('whatever');. However, the ...

Is there a way to use a single url in Angular for all routing purposes

My app's main page is accessed through this url: http://localhost:4200/ Every time the user clicks on a next button, a new screen is loaded with a different url pattern, examples of which are shown below: http://localhost:4200/screen/static/text/1/0 ...

Is there a way for me to verify the type of these objects?

Could you help me verify the type of obj below? Question 1: const arr = ["a", "b", "c"] is equivalent to arr = { 0: "a", 1: "b", 2: "c" } Is this correct? Question 2: My object is const myObjects = { "1111": { isField: true, mySet: [" ...

Jquery doesn't support adding click events to dynamically generated list items

Jquery Code Here is my jQuery code that retrieves a dynamically generated list of categories from a database. I have added a click event to each list item, but it is not triggering the alert as expected. <script type="text/javascript" src="<?= base ...

Guide to positioning two labels dynamically around an image: one on the right and one on the bottom

I am in the process of creating a visual list of search results and I am struggling with formatting it properly. . I have tried to implement the following code for the web form: <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat=" ...

What is the best way to embed a webpage into another webpage using frames?

I need help embedding an iframe on my website, , to display the items I am selling on eBay. This is my first time attempting to create an iframe. ...

Adding a trailing slash to the URL in an Express server causes it to be appended

I've encountered a strange issue with my express server. Whenever I try to use a specific URL route with the word 'bind,' an extra '/' is automatically added to it. This behavior isn't happening with other URLs that I've ...

What is the advantage of using event.target over directly referencing the element in eventListeners?

Suppose there are several buttons in an HTML file and the following code is executed: const buttons = document.querySelectorAll('button'); buttons.forEach((btn) => { btn.addEventListener('click', (e) => { console.log(btn.te ...

Tips for identifying modifications in an input text field and activating the save button

Currently, I am developing a function that can detect any changes made in the text field and then activate the save button accordingly. This code is being executed in Visual Studio 2017, using HTML and JavaScript. (function () { var customer_addres ...