Animate CSS during page load

Currently, I am implementing AJAX to dynamically load pages on my website. During the loading process, I wish to incorporate a spinning animation on the logo to indicate that content is being fetched.

The jQuery script (although I'm still learning and unsure if it's JavaScript or jQuery) responsible for managing the CSS modifications when a page is loading looks like this:

function loadPage(url)
{
url=url.replace('#page','');

$('#logo').css('display','none;');

$.ajax({
    type: "POST",
    url: "load_page.php",
    data: 'page='+url,
    dataType: "html",
    success: function(msg){

        if(parseInt(msg)!=0)
        {
            $('#pageContent').html(msg);
            $('#logo').css('visibility','visible');
        }
    }

The keyframes specified in the CSS control the spinning animation as follows:

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

#logoholder { 

}

#logo   { 
    background: url(images/logo.png) 0 0 no-repeat; 
    width: 130px; 
    height: 130px; 
    animation-name: spin; 
    animation-duration: 1000ms; /* 1 second */
    animation-iteration-count: infinite;
    animation-timing-function: linear;

    -o-animation: none; /* Placeholder for Opera support */
}

I am currently seeking a way to adjust the CSS properties via jQuery so that the logo only spins while the page is loading, without continuous rotation during idle times. Is there a specific attribute that can be added or removed within the "#logo" selector to achieve this effect?

Any advice on how to dynamically enable or disable the spinning animation based on the loading status of the page would be greatly appreciated!

Answer №1

To avoid directly binding the CSS animation to #logo, you can instead bind it to a specific class such as .rotate. Start by adding the rotate class to #logo when Ajax begins using the addClass method:

$("#logo").addClass("rotate");

Once the ajax content has finished loading, simply remove the class using the removeClass method:

$("#logo").removeClass("rotate");

This approach allows you to display the logo in its regular state and apply the spinning animation only when Ajax is active.

Answer №2

Try using <body class="loading"> on your webpage.

At the end of the page, include

$('body').removeClass('loading');

To apply your css animation, use the selector body.loading #logo.

Once everything is loaded, jquery will remove the .loading class and stop the animation.

When using $.ajax, make sure to add the .loading class in the beforeSend method and remove it in the complete event.

Answer №3

It appears that the logo is initially hidden and then displayed after the content loads, as specified in the comments. However, you are using both the display and visibility properties to hide it, which are not the same. I have made some adjustments to your functions below, using only the display property for consistency. Give this revised version a try, it should point you in the right direction.

function loadPage(url)
    {
    url=url.replace('#page','');

    $('#logo').css('display','block');

    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        dataType: "html",
        success: function(msg){

            if(parseInt(msg)!=0)
              {  
               $('#pageContent').html(msg);
               $('#logo').css('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

Troublesome tab key function in FireFox causing navigation issues

I'm facing an issue with the tab key focus on links in FireFox. Strangely, it's working fine in Chrome but in FireFox, it keeps looping within one element. For better understanding, I have created a demo showcasing this behavior specifically in ...

Ways to calculate the total number of keys within a JSON object at a certain level

I need to process a JSON file by extracting values from keys located at a specific depth. The initial value I want to access is situated here: json.children[0].children[0].children[0] Is there a method to navigate through the JSON object at a particular ...

Is it possible to add a border to both the tbody and td

I currently have a table that is organized with tbody elements to group rows together. In order to create a grid-like structure, I applied borders to each individual td element within the tbody. However, I also desire to show that the tbodies themselves ar ...

Automatically synchronize dynatable with AJAX requests and JSON responses

I'm currently facing a bit of confusion as my code appears to be functioning properly, but the table is not updating as expected. xD I am fetching data from my database and loading it into a table, aiming for it to automatically update every three se ...

Breaking down the initial state within a redux reducer

Can you explain the distinction between returning state in the default case of a Redux reducer using return state and return { ...state } ? ...

I am struggling to trigger my $.ajax request when clicking a button

I'm facing an issue with my variables in the Ajax request. When I place the request outside of the .on() event, everything works fine. However, when I move the same request into the .on(), it stops working. Any assistance on this matter would be highl ...

What is the process for building an interactive quiz using JavaScript?

In the process of creating a quiz, I envision a user interface that presents questions in a "card" format. These questions will include simple yes/no inquiries and some multiple-choice options. As the user progresses through the quiz, their answers will de ...

Creating visual content on Canvas using JavaScript

Having an issue with stacking multiple images on separate Canvas layers, and they're not drawing on the canvas. Can anyone help me figure out what I'm missing? Thanks CSS .positionCanvas{ position: absolute; left:0; righ ...

How to effortlessly bring modal windows onto the page from either side

I'm looking to have two hidden modal windows that can slide in from the left and right edges of the page. In my current setup, the left modal slides in from the right instead of sliding out from the left margin as desired. You can view the issue in th ...

Enrich your HTML using AngularJS

CSS <div class="container"> <section> <p>{{content}}</p> </section> </div> script.js (function () { var script = function ($scope){ $scope.content = "example"; } }()); I am currently ...

Incorporate a JavaScript message using C# code in the backend

I am looking for a code snippet to execute a JavaScript function called recordInserted() that displays an alert, within the add_Click method in my code-behind file. Here is the relevant section of my code: protected void add_Click(object sender, EventArgs ...

JavaScript and PHP open-source libraries for enabling voice chat functionality

Seeking assistance on incorporating voice-chat functionality into my website through PHP and JavaScript. Are there any open-source libraries available for this purpose? I am willing to utilize Flash if necessary, but limited to using only Flash, JavaScri ...

Utilize Moment in React-Native to adjust the time zone format for the Vi Locale

I'm encountering a date formatting issue while using Moment in React-Native. Specifically, I have formatted the date to an English locale, but now I want it to also support the Vietnamese locale: Moment('2022-09-02T02:00:00+00:00') . ...

Upon the second attempt, AJAX encounters an error while trying to access the URL, forcing a page refresh

My AJAX function works flawlessly on the first request, but encountering a strange issue on the second request where the site reloads without any errors showing up in Chrome DevTools. The network tab doesn't register the request, and neither the succe ...

Creating a selection area with CSS that appears transparent is a straightforward process

I'm currently exploring ways to implement a UI effect on a webpage that involves highlighting a specific area while covering the rest of the page with a semi-transparent black overlay, all using CSS only. What is the most common approach to achieving ...

The Discord.js guildMemberUpdate event: Everything you need to know

I am currently working on creating a welcome message bot using Discord.js. The goal is to have the bot send a welcome message through a webhook in a specific channel when a member gains access to it. Here's what I have so far: client.on("guildMe ...

Encountering unexpected behavior with the .data() method

I'm encountering an issue with my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv= ...

Tips on using object fields as directive arguments in AngularJS

Is there a way for me to pass an object array to a directive and have it output specific fields that I specify at the location where I use the directive? Let's look at an example: //directive app.directive('MyDirective', function() { ret ...

Conceal information from view without hiding pseudo content visually

Here's a method I'm using to incorporate icons through the use of :before or :after pseudo content: <span class="icon icon__example is-content-visually-hidden">assistive text</span> Is there a way to hide the assistive text visually ...

Master the art of returning two functions within a single function in Javascript with NodeJS and ExpressJS

Currently, I am facing an issue where I need to combine two objects and return them in one function. The challenge lies in the fact that both objects have almost identical properties, but different values. To tackle this problem, I created two separate fu ...