The conclusion of jQuery's nested animation

Functioning, But Page Transitions Inconsistent

What I Believe to be the Correct Approach, But Event Triggering Issue

Currently, I am experimenting with page transitions and utilizing bind() to detect the end of the CSS event transition. When a class is added, the transition occurs and is removed upon completion, which triggers the code in the bind() callback. However, only one bind callback is working, leading to incomplete transitions.

Jquery

$("a").click(function () {
    // Code to handle page transitions
});

I am seeking a solution without relying on third-party plugins, and a detailed explanation of why this inconsistency is occurring would be greatly appreciated.

SOLUTION

Demo with Solution

Answer №1

It seems likely that your code is running in the following sequence:

  1. The first function is bound to all elements with the class 'active1' at the time of the bind, including those available when the bind() function is called.
  2. The second function is bound to all elements with the class 'active2' at the time of bind(), similar to the first function.
  3. The first function is triggered.
  4. The class on the container is changed to 'active2', even if the element was not part of the list obtained in step 2.

According to the JQuery specifications:

Event handlers are only bound to currently selected elements, which need to exist on the page at the time of the bind() call.

Would you consider moving the second bind inside the first function or executing the functionality there instead?

function bindElement(elem, id) {

    $('.active1').on("webkitAnimationEnd oAnimationEnd msAnimationEnd animationend", function (event) {

        alert("first");
        if (event.animationName === "three") {
            console.log('the event happened');
        }

        elem.removeClass('active active1');
        $(id).addClass('active');

        $(this).off("webkitAnimationEnd oAnimationEnd msAnimationEnd animationend");
    });
}

function bindContainer(id) {

    $(id).addClass('active active2');
    $('.active2').on("webkitAnimationEnd oAnimationEnd msAnimationEnd animationend", function (event) {

        alert("second");

        $(this).removeClass('active2');
        $(this).off("webkitAnimationEnd oAnimationEnd msAnimationEnd animationend");
    });
}

$("a").click(function () {

    // Set the effect type
    var effect = 'slide';

    // Set the options for the effect type chosen
    var options = {
        direction: $('.mySelect').val()
    };

    // Set the duration (default: 400 milliseconds)
    var duration = 500;
    var $sel = $('div.active');
    var $class = $(this).attr('class');

    if ($class === 'Home') {

        $sel.addClass('active1');
        bindElement($sel, '#Container1');
        bindContainer('#Container1');
    }

    if ($class === 'Services') {

        $sel.addClass('active1');
        bindElement($sel, '#Container2');
        bindContainer('#Container2');
    }

    if ($class === 'About') {

        $sel.addClass('active1');
        bindElement($sel, '#Container3');
        bindContainer('#Container3');
    }

    if ($class === 'Contact') {

        $sel.addClass('active1');
        bindElement($sel, '#Container4');
        bindContainer('#Container4');
    }

});

DEMO

Answer №2

For a smooth animation experience, consider using the JQuery transit plugin found at:

This plugin utilizes CSS3 animations whenever possible and is compatible with most browsers. You can easily animate elements like this:

$('div').transit({opacity: .5, rotateX: 30, delay: 200}, 2000, function(){

   // ANIMATION FINISHED    

});

You can also chain multiple animations together like so:

$('div').transit({scale: 3.2}).transit({x: 300, y: 400}).transit({x:400, y: 500});

Remember to keep your code organized and avoid posting messy questions with incomplete code snippets.

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

Exploring MySQL: Retrieving Data Efficiently while Monitoring Server Performance

Currently, I am in the process of developing a web application that communicates with a MySQL database to send and receive data. My goal is to display any changes made in the database on the web page as quickly as possible. My main concern now is determin ...

Limited functionality: MVC 5, Ajax, and Jquery script runs once

<script> $(function () { var ajaxSubmit = function () { var $form = $(this); var settings = { data: $(this).serialize(), url: $(this).attr("action"), type: $(this).attr("method") }; ...

Utilizing Local Files in a Bootstrap Video Carousel

Currently working on a website for a school project and I am trying to incorporate a carousel of videos. After finding a bootstrap carousel template that played videos linked to a server, I attempted to switch it out with local video files without succes ...

The Wordpress plugin's Ajax function is giving back a response value of zero

I'm facing an issue where I am attempting to send AJAX data to my wordpress table, but the response I receive from my PHP script is always a 0. This problem arises specifically on the admin side of things. Can anyone provide assistance? Furthermore, ...

Tips for aligning a select and select box when the position of the select has been modified

Recently, I encountered an interesting issue with the default html select element. When you click on the select, its position changes, but the box below it fails to adjust its position accordingly. https://i.stack.imgur.com/SwL3Q.gif Below is a basic cod ...

Creating a loading screen with JQuery for an Ajax request

I want to implement a loading screen in my web application every time an ajax call is initiated using jQuery. Currently, I have different types of ajax calls such as $.getJSON, $.post, and $.ajaxFileupload, each with their own Success function that remov ...

Updating the Origin of a Sound Element in HTML5

I am currently working on developing a personalized browser-based music application. I am at the initial stage of this project where my main goal is to create a button that allows users to change the song being played by an HTML5 audio player. I have attem ...

if this.className includes Ajax completion

I am currently trying to determine if the user has clicked on the confirm or deny link. If they have clicked confirm and it is complete: idurl = '<?php echo $siteUrl ?>/profile.php?userid='+id+''; if (idurl == window.location.hr ...

Darken the backdrop of the bootstrap modal

When the modal is opened, the background turns black instead of having an opacity of 0.5 or something similar. How can I resolve this issue? I am using Bootstrap 3.2. Below is some CSS code snippet: .fade.in { opacity: 1; } .modal-open .modal { overflow ...

The logo appears perfectly sized in CodePen, but it seems oversized in the Outlook email template

My email template (HTML with inline CSS) features a logo with a fixed width and height (px). Oddly, after receiving an email using this template, the logo expanded to fill the entire page width. I attempted to add !Important to the width and height propert ...

A guide to retrieving a JSON Object using Python within the Flask Framework

How do I extract a JSON Object using Python within the Flask Framework? Check out this snippet of code ` var hotel=$( "#listHotel option:selected" ).val(); if(hotel!="select") { $.ajax({ url: '/getHot ...

Angular: module instantiation unsuccessful

Just getting started with Angular and running into an issue where the module seems to be unavailable. https://docs.angularjs.org/error/$injector/nomod?p0=plopApp My code is very basic at the moment, just setting things up: @section scripts{ <script s ...

Adjust the color of the text as it scrolls by

As I work on developing my website using the Neve Theme on WordPress, I have encountered an issue with customizing the header block. I am using a plugin to set a background color for the header after scrolling 100px down the page, but this makes the text h ...

Sending a variable to a function within a jQuery AJAX success callback

I'm currently facing an issue trying to preload images using a jQuery AJAX call. The problem arises when attempting to pass a URL string into a function within the success function of the AJAX call. Here's the code I have so far: // Image prelo ...

"Enhance your website with dynamic PHP Ajax live search and infinite scrolling

When scrolling to the bottom of the .dropdown-menu, I would like to load an additional 7 rows from the database. However, I haven't been successful in implementing this feature using the script provided. I am currently utilizing Bootstrap CSS and JS f ...

The most efficient method for interacting with externally generated HTML in Rails

For my Rails page help documentation, I utilized the HelpNDoc help authoring tool to generate a folder containing HTML pages and additional folders for JavaScript, images, stylesheets, and libraries. Now I am seeking the most efficient way to integrate thi ...

Discovering the trigger for a CGI script execution

My goal is to enhance the functionality of my CGI script by implementing different actions based on the trigger that initiated the script. For instance, if a particular button is clicked, I want to clear a database. If another button is pressed, I aim to ...

Picking an item for alignment at the center with flexbox styling

What was the reason for specifying .footer-background to have a display: flex property in order to center the .copyright-text? Why did setting display: flex on .footer not achieve the desired result? .footer-background { background-color: #00008B; ...

Design a square confined within an adjustable rectangular container

Looking to fill a rectangular area on my page with a square positioned in the center, regardless of whether the rectangle is horizontal or vertical. Many existing solutions focus solely on creating a square from the width of a containing box, which doesn&a ...

The hover effect is failing to impact a child element during a transition

I created a link with an arrow next to it that should move 20px to the right when hovered over, using a transition of 1s. However, for some reason the transition is not affecting the arrow. Can anyone please assist me in figuring out why this is happenin ...