Generate an additional CSS animation to display before the next page is loaded

When loading a page, I am implementing CSS transitions to display animated lines. To activate these lines, I am utilizing a jQuery script to change the class on the line elements.

I am looking to reverse the CSS transitions when transitioning to the next page. Currently, the lines draw up on pageload, but for the next page, I want them to draw out before drawing them in again. This might require using something like AJAX to load pages seamlessly and ensure smooth transitions between pages from the start.

Demo:

HTML:

<html lang="en" class="noTouch">
 <!-- META DATA -->
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">

  ...

</code></pre>

<p>CSS: </p>

<pre><code>.h-line {
 ...
 }

 ...

$(function () {
$('.v-line, .h-line').addClass('ready');
});

Answer №1

If you want to incorporate a seamless transition effect in your website, consider using the transitionend event. This event is triggered when a CSS transition finishes.

To ensure that clicking on a link does not immediately navigate away from the current page, you can prevent the default behavior of the link click event.

//Attach click event listener to <a> elements
$('a').click(function(e){
    //Stop the default navigation behavior
    e.preventDefault(); 

    //Save a reference to the clicked link
    var clickedLink = this;

    $('.v-line, .h-line')
        //Remove CSS classes (consider adding reverse transitions as well)
        .removeClass('ready')

        //Listen for the "transitionend" event.
        .one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',   
            function(e) {
                //Navigate to the clicked link's URL after the transition completes
                document.location.replace(clickedLink.href);
            }
        );
);

If you aim to achieve a smooth transition between pages without fully reloading them, utilizing AJAX technology enables you to update specific content on the page dynamically.

Answer №2

This particular solution utilizes Event Delegation. Here, JQuery captures the click on the element and then eliminates the CSS class.

By implementing the code below, when the user clicks, the animation will play in reverse and trigger a page change. However, the page alteration may disrupt the animation flow.

Access your page, insert the following code into the browser console, and then click on a link to witness its functionality.

$("body").on("click","a",function( event ){ 
$('.v-line, .h-line').removeClass('ready');});

To ensure that the entire CSS animation finishes before transitioning to another page, incorporating Event Prevent Default might be necessary. This action prevents the original click from proceeding, requiring manual initiation of the event post-animation completion.

Visit your page, input the given code in the browser console, and click on a link to observe it in operation.

Take note that due to the prevention of default behavior, there is no immediate page switch; instead, the animation is granted sufficient time to conclude.

$( "body" ).on( "click", "a", function( event ) {
event.preventDefault();
$('.v-line, .h-line').removeClass('ready');});

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

Is there a way to trigger a modal popup when hovering over a Bootstrap 5 card?

After coming across a handy template online, I decided to implement a modal pop-up feature when hovering over cards using Bootstrap 5. Here's what I have so far: class SavedEpisodes extends Component { $(function() { $('[data-toggle=&qu ...

Sending a jQuery variable to an external PHP script

Utilizing the HTML5 Geolocation API, I am able to retrieve latitude and longitude coordinates which I then need to pass to an external PHP file. This PHP file (getPosition.php) retrieves JSON variables from my database, allowing me to utilize them in geo.j ...

Tips for appending a custom list item to the bottom of the jQuery autocomplete results

I am using jquery autocomplete and have initialized the following variables $("#some_id").autocomplete("search.php?in=somewhere", { width: 270, selectFirst: false }); $('#some_id').setOptions({max: 5}); The current setup returns ...

"Troubangular: Troubleshooting unexpected behavior when trying to update ngFor after setting a property

Dealing with what seems like a straightforward component here. I'm fetching an array of objects from an API, storing them in a property, and then displaying them in a select list for the user to choose from. When the value changes, I filter the result ...

Unable to divide using " " in JavaScript

When attempting to split my data using .split('\'), I encounter an error message saying "Uncaught SyntaxError: Invalid or unexpected token". Is it possible to split with '\'? ...

Adjust the line height of the paragraph class using Bootstrap customization

Currently, I am working with Bootstrap V4 and I am facing an issue where I need a customized paragraph to have a line-height of 1. However, I have been unsuccessful in overriding the default Bootstrap setting of approximately 1.5. <p class="ptim ...

There was a problem retrieving the product information from the API

I have been struggling to pinpoint the exact issue at hand. The foundation of HTML and CSS is pre-written, with JavaScript responsible for generating the core elements to populate the DOM. Within my script, I am attempting to retrieve product data in ord ...

Is it possible to configure Webpack/NextJs to handle a recurring import path mapping?

Exploring ways to streamline imports in my NextJs Project (v14.1.3), I'm interested in finding a method to modify the import statement below: import foo from 'path/to/foo/foo' To possibly simplify it to: import foo from 'path/to/foo&ap ...

Using Ruby variable as a parameter in JavaScript

Is there a way to include a ruby variable as a parameter in a JavaScript function for a Rails checkbox? Here is an example of what I'm trying to do: <%= check_box_tag 'Sugestão', prato.id , prato.sugestao,:class => prato.categoria_pr ...

Observer function simulated by SinonStub

I am currently testing express middleware using sinon.js My goal is to verify that it sends a specific JSON response and prevents the request from moving on to the next middleware or request handler. const middleware = (req: Request, res: Response, nex ...

How can I set a default radio button selection when the page is loaded?

Input your radio button code below: <ion-radio ng-model="data.gender" ng-required="true" ng-value="'male'">Male</ion-radio> <ion-radio ng-model="data.gender" ng-required="true" ng-value="'female'">Female</ion-rad ...

What is the process for setting up a redirect to the login page in ASP.NET?

When using ASP.NET, what is the most effective method for redirecting a user to the login page if they try to access a page intended for registered users? Please note that although I am utilizing ASP.NET WebForms, there are no actual WebForms involved. Th ...

Script for automated functions

I have implemented 4 functions to handle button clicks and div transitions. The goal is to hide certain divs and show one specific div when a button is clicked. Additionally, the background of the button should change upon hovering over it. Now, I am look ...

Enhance your li items with prev and next buttons using PHP or jQuery

I am working on a project where I have a list being populated dynamically with PHP. My goal is to include previous and next buttons within each list item. When an item is clicked, it opens a jQuery modal with a specific href value. How can I add this href ...

Gracefully Switching Between Various Functions

Suppose I have a collection of functions that perform various tasks: function doSomething() { console.log('doing something'); } function accomplishTasks() { console.log('accomplishing tasks'); } function executeAction() { console. ...

What steps should I take to successfully implement a div ID selector in an HTML document?

<div id="nofries"> <h1 class="restaurant mt-5 ms-3">CRAZY GREEKS</h1> <h3 class="ms-5">GREEK FOOD, SUBS &amp; PIZZA</h3> </div> I'm having trouble with my div ID "nofries" not ...

Is it possible to modify the dropdown menu so that it opens on the right side instead of using a select tag?

Is there a way to make the drop-down from the select tag open to the right side(drop-right)? <label for="ExpLabel">Select Message Expiry:</label> <select name="ExpSelect" id="Expiry"> <option value="ExpiryDate">1 Day</opt ...

What is causing the malfunction in this code? (Regarding the key and value variable objects)

var elements = []; var attribute1 = $(index).attr('class'); //or any string var attribute2 = $(index).html(); //or any string elements.push({ attribute1: attribute2 }); When I run this code, the output I receive is: this Why am I unable to set ...

Struggling to access specific data within a JSON object? Wondering how to extract and display data from a JSON object in VUE?

Despite my extensive searching on Stack and the internet, I have not been able to find a solution to my problem. Currently, I am attempting to retrieve data from a JSON file located in the Vue src folder. The file contains three arrays with names that inc ...

Is there a way to convert inline styling to CSS using Material-ui within a React component?

In my development work with React, I initially utilized inline styling to customize the appearance of material-UI components. However, I have come to realize that utilizing CSS provides a more structured and efficient approach to styling components. Despit ...