The CSS transition fails to animate with dynamically loaded Ajax content

My journey into the world of CSS animations is relatively new, with some previous experience in JQuery. However, I encountered a problem with choppy animations on smaller devices like my iPad, nook, and android phone. To address this issue, I decided to use a CSS transition instead by toggling a class with jQuery.

When the ajax content is loaded, a hidden class is applied to the <section> tag that contains the desired portion of the page. This allows for full markup in case JavaScript is disabled, but when enabled, it grabs the <section id="sectionId">. The sectionId varies from page to page, such as <section id="home"> for index.html, <section id="contact"> for contact.html, and <section id="products"> for products.html. For demonstration purposes, let's use sectionId.

This is how it should work:

  1. Replace the content in #content through ajax, obtaining the #sectionId from the URL linked by the navigation.

  2. Add the hidden class once loaded.

  3. Remove the hidden class so that the CSS transition triggers, causing the loaded section to slide in from the right.

Here's the key code:

HTML:

<div id="content">
    <section id="sectionId">
        <!-- Content Here -->
    </section>
</div>

CSS:

#content > section
{
    margin-left: 0;
    -moz-transition: .5s all ease-in;
    -o-transition: .5s all ease-in;
    -webkit-transition: .5s all ease-in;
    transition: .5s all ease-in;
}

#content > section.hidden
{
    margin-left: 2000px;
}

JQuery:

function loadPage(container, url, sectionId) {

    container.load(url + ' #' + sectionId);
}

$(document).ajaxComplete(function (event, xhr, settings) {
    $('#content > section').addClass('hidden');        
    $('#content > section').removeClass('hidden');
});

While this works for the index page and #home section only, the sliding transition appears to malfunction on other pages after the hidden class is removed. Despite the intended removal of the class, the transition fails to occur on these pages. The developer tools reflected the addition and removal of the class correctly, yet the transition did not execute. It was observed that leaving the home page without navigating away resulted in the correct transition upon returning. Essentially, the site reloads the home page content via ajax regardless of the link clicked.

If there are better CSS methods to achieve this or suggestions on improving the addition of the hidden class, please share your insights. The momentary display of content before adding the class due to browser variations and connection speeds presents another challenge worth addressing.

Answer №1

In my opinion, the approach should involve

function displayContent(element, address, partId) {
    $('#main-section > division').addClass('invisible');        
    element.load(address + ' #' + partId);
}

$(document).ajaxCompletion(function (activity, request, configurations) {
    $('#main-section > division').removeClass('invisible');
});

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

"Troubleshooting Problem with jQuery UI Autocomplete and JSON Data

I have implemented the jQuery UI Autocomplete feature using the code below. <script> $(function() { $( "#city" ).autocomplete({ source: function( request, response ) { $.post('<?php echo base_url()?>records/get_villa ...

Guide to initiating an AJAX request to the GraphHopper Route Optimization API

Currently, I am exploring the functionalities of GraphHopper Route Optimization API for solving Vehicle Routing Problems (VRP) with pickups and deliveries. To test it out, I am using an example from . The specific request I am making is as follows: var ...

Having trouble retrieving the Post value ID from row table in PHP using Jquery

Although I have searched extensively for a solution to my problem, none of the suggested fixes seem to work for me. The issue is quite simple - I need to retrieve the item-id value and then POST it to del.php. However, I am unable to access the POST value ...

Finding the index of the maximum value in an array with a uniform distribution, especially in cases where there are multiple maximum values and the quantity of max values is already

I recently came across an interview question that did not have a satisfactory solution. The question posed was: Given a vector of integers, the task is to find the index of the maximum value. However, if there are multiple maximum values in the vector, ea ...

Obtain data from a dynamic website using AJAX (XMLHttpRequest) in C#

I am working on developing a C# application to retrieve my data cap details from my ISP's website. The specific page I am refering to can be found here, however, I suspect that it may only be accessible within their network. If you require more inform ...

set data attribute using jquery

Here's an example of a DIV tag: <div id='idval'></div> In jQuery, you can do the following: var data1 = '<b>' + Text for file + '</b>' To use the .data method to assign the content of d ...

Error encountered due to Ajax JSONP not found

Almost there with getting json data, but encountering an error: SyntaxError: missing ; before statement Here is what my json data looks like: {"custom1":"","dateOfBirth":null,"custom2":"","custom3":"","custom4":"","custom5":""} Using jsonp has helped me ...

displaying large datasets on server-side using jQuery DataTables

I am currently utilizing DataTables 1.9.4 along with several plugins (ColVis, ColReorderWithResize, and ColumnFiltering). The parameters configured for server-side communication include: "bServerSide" "fnServerData" "sAjaxSource" Say I need to present 3 ...

Ways to layer div elements in HTML

Hey there! I am currently in the process of learning how to create a simple website. So far, I have successfully created some static web pages and now I am attempting to add a dynamic element with CSS. As part of this effort, I decided to experiment with t ...

Why won't my HTML/CSS/JavaScript modal pop up in front of everything else?

Attempting to create my own modal in JSFiddle has been an interesting challenge for me. Once I had something that worked, I tried implementing it on my website. Unfortunately, the modal appears behind other elements on the page. What could be causing this ...

Tips for adjusting the number of columns in a list display using CSS

Recently, I decided to delve into the world of CSS and Bootstrap. I came across an interesting example that I'm trying to implement in my project. You can find the example here. Here's the HTML code snippet: <div class="container"&g ...

Tips for eliminating delays in removing CSS transitions

Is there a way to smoothly transition a CSS property, make an immediate change in the CSS property value, and then reattach the transition without any delay? To better understand, consider the following example: if ($(".marquee").height() < $(".marqu ...

yii CGridView refresh trigger an influx of Ajax requests

Does anyone know why the yii cgridview refresh button triggers multiple ajax calls? Upon refreshing, I have noticed that it initiates several ajax calls (this time it's 3, but sometimes it's 4 or even 5) GET http://localhost/ijob-css/index.php/ ...

Retrieving data from an XML file using an Ajax request

I am using a native AJAX request to insert node values into HTML divs. However, I have noticed that when I update the XML values and upload them to the server while the website is running, Chrome and IE do not immediately reflect the changes (even after re ...

Executing multiple setTimeout calls simultaneously can result in greater delays than anticipated

During my exploration of Node performance, I encountered an unexpected issue where an example involving 50 concurrent calls to setTimeout took over 4 seconds instead of the anticipated 500ms. The scenario involves a basic express server that waits for all ...

Steer clear of using React Ajax in combination with FileUpload and FormData if you want to prevent the

Upon completing an AjaxCall using formData and files_upload, the entire page inexplicably reloads. This issue does not occur with a standard Ajax Call. It's important to note that there is no FORM tag present in the codebase as we are utilizing REACT ...

an occurrence of an element being rendered invisible, within an instance of an element being rendered as flexible,

I am trying to create a button that will show a list of elements. Within this list, there is another button that can be used to either close the list or hide it entirely. However, for some reason, my code is not functioning properly. let btn1 = documen ...

Persistent banner designed to accompany blog posts

I've been attempting to insert a banner ad that runs along the left side of my blog post. Unfortunately, all my efforts so far have caused it to display above the content, pushing the blog post down the page. You can view the live link here. Here i ...

What is the best way to position a button to the right side of the screen?

Is there a way to align a button to the right of a page? I attempted using text-align: right; and align-content: right; In addition, I want the button to utilize display: flex; #hideShow { display: flex; text-align: right; align-content: right; ...

Style applied directly to .col elements

I want to customize the styling of the .col elements on a single page without affecting other pages. I've tried adding the following code snippet to my CSS file: .col { border: 1px solid #7851A9; } However, this also styles the element ...