Switch up the animation based on the state in AngularJS

In my AngularJS application, I have implemented CSS animations for transitioning between states. The animations involve sliding content from left to right with specific timings and transforms.

.content.ng-enter,
.content.ng-leave
{
    -webkit-animation-timing-function: ease-out;
    -webkit-animation-duration: 350ms;
    -moz-animation-timing-function: ease-out;
    -moz-animation-duration: 350ms;
}
.content.ng-leave
{
    -webkit-transform: translateX(-100%);
    -webkit-animation-name: slideouttoleft;
    -moz-transform: translateX(-100%);
    -moz-animation-name: slideouttoleft;
}
.content.ng-enter
{
    -webkit-transform: translateX(0);
    -webkit-animation-name: slideinfromright;
    -moz-transform: translateX(0);
    -moz-animation-name: slideinfromright;
}
.content.ng-leave.reverse
{
    -webkit-transform: translateX(100%);
    -webkit-animation-name: slideouttoright;
    -moz-transform: translateX(100%);
    -moz-animation-name: slideouttoright;
}
.content.ng-enter.reverse
{
    -webkit-transform: translateX(0);
    -webkit-animation-name: slideinfromleft;
    -moz-transform: translateX(0);
    -moz-animation-name: slideinfromleft;
}

However, I want the animation direction to change for specific navigations, such as moving from Section 3 to Section 2 or 1. This requires applying a different animation based on the current state and the next state of the application.

I currently have the ability to determine the current state using classes on the body tag:

<body ng-class="{ section1 : $state.includes('section1'), section2 : $state.includes('section2'), section3 : $state.includes('section3') }">

Is there a way to dynamically apply the correct animation class based on the transition between states in AngularJS?

Answer №1

Here is a snippet of code that I've found to work effectively:

$rootScope.$on("$stateChangeStart", function(evt, toState, toParams, fromState, fromParams) {
    $('html').removeClass('goLeft').removeClass('goRight');

    if ((toState.name === 'section1' && fromState.name === 'section2') || (toState.name === 'section1' && fromState.name === 'section3')) {
        // go left
        $('html').addClass('goLeft');
    } else if ((toState.name === 'section3' && fromState.name === 'section1') || (toState.name === 'section3' && fromState.name === 'section2')) {
        // go right
        $('html').addClass('goRight');
    } else if (toState.name === 'section2' && fromState.name === 'section1') {
        // go right
        $('html').addClass('goRight');
    } else if (toState.name === 'section2' && fromState.name === 'section3') {
        // go left
        $('html').addClass('goLeft');
    }
});

I then utilize prefixes for different CSS animations:

[ui-view=content].ng-enter,
[ui-view=content].ng-leave {
    -webkit-animation-timing-function: ease-out;
    -webkit-animation-duration: .2s;
    -moz-animation-timing-function: ease-out;
    -moz-animation-duration: .2s;
}
.goLeft [ui-view=content].ng-leave {
    -webkit-transform: translateX(-100%);
    -webkit-animation-name: slideouttoleft;
    -moz-transform: translateX(-100%);
    -moz-animation-name: slideouttoleft;
}
.goLeft [ui-view=content].ng-enter {
    -webkit-transform: translateX(0);
    -webkit-animation-name: slideinfromright;
    -moz-transform: translateX(0);
    -moz-animation-name: slideinfromright;
}
.goRight [ui-view=content].ng-leave {
    -webkit-transform: translateX(100%);
    -webkit-animation-name: slideouttoright;
    -moz-transform: translateX(100%);
    -moz-animation-name: slideouttoright;
}
.goRight [ui-view=content].ng-enter {
    -webkit-transform: translateX(0);
    -webkit-animation-name: slideinfromleft;
    -moz-transform: translateX(0);
    -moz-animation-name: slideinfromleft;
}

Although effective, the code may seem cumbersome due to the need for conditionals for each possible navigation path.

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

How can you enable the sortable feature with a mousedown event instead of a drag event by utilizing Jquery UI Sortable?

I have implemented the Jquery UI Sortable plugin to enable re-ordering of table rows. Currently, the drag and drop functionality is working well, but I would like to trigger the sortable feature using a mousedown event instead of a drag event. You can vi ...

Solving synchronization issues when updating and selecting MySql data in a Node.js environment with concurrent multiple requests

Currently, I'm using expressJS with an endpoint that connects to a MYSQL database. This endpoint executes a query to retrieve data and assigns the first result to a user by updating a specific field with the user ID. The rule is that each row can onl ...

Storing AngularJS route components in the cache for optimal performance (keep-alive)

I'm looking for a way to cache the state of a component A so that it doesn't re-render every time I navigate away and back to it. This component also makes a slow API call in its constructor. I want to maintain this initial state throughout the u ...

I'm running into a "timeout" issue with socket.io and a self-signed SSL connection. Can anyone help me troubleshoot this?

After setting up a nodejs server with HTTPS, everything seems to be working fine when sending GET requests. However, I encountered an error message 'WebSocket was closed before the connection was established' when trying to connect another nodejs ...

What is the best way to handle the return value from using indexOf on a string?

I am looking to manipulate the value returned by a specific conditional statement. {{#each maindata-hold.[2].qa}} <div class="section"> <a href="javascript:void(0)" class="person-link" id="{{id}}"> <span class="name- ...

Is it possible to implement jQuery events on all elements belonging to a specific class

Hey there, I'm facing a little challenge with my code. Currently, I have a snippet that allows a user using iOS to drag around a <div> with the class .drag on the webpage. Everything works perfectly when there's only one instance of .drag, ...

Error: 'err' has not been defined

Recently, I embarked on creating my very first API using Mongo, Express and Node. As I attempted to establish an API endpoint for a specific user, the console threw me this error: ReferenceError: err is not defined. Curiously, the same method worked flawle ...

Tips on incorporating multiple lines of placeholder text into a textarea field:

Is it possible to add multi-line placeholder text in a textarea? I found this solution, but unfortunately it does not work on Mozilla and Safari. It seems Chrome is the only browser where this method works: $('#nameTxtBox').attr("placeholder", ...

Is there a way to mix up the sequence of information when replying?

There have been numerous instances where this question has been raised and addressed, such as here, here, and here. However, those discussions pertained to Bootstrap 4, which is approaching its End of Life as of today, 2021-10-20. Given that Bootstrap 5 i ...

Abort S3 file upload using ASW-SDK

Is there a way to abort an upload without raising an error Upload aborted. when calling upload.abort()? import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3'; import { Progress, Upload } from "@aws-sdk/lib-storage"; cons ...

What is the most effective way to access content from a webpage that is rendered

Is there a reliable way to download from links on a JavaScript rendered webpage using Python as the preferred language? I have attempted to use the Selenium Python bindings on a headless server, but it has proven to be slow, error-prone, and unable to acc ...

Using Angular's factory method in combination with the $http service

I am in the process of creating a factory that retrieves data from a JSON feed and returns the results. Below is the factory using $http nearestLocationApp.factory("allTheLocationsFactory", function($http){ var locations = "Not sure why it's not ...

Issue 500 encountered while implementing VB.NET with jQuery AJAX

Having trouble populating a select option using jQuery ajax, and I could really use some assistance! Encountering the following error: Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:20440/admin ...

When working with JavaScript and Node.js, it is not possible to access an object's index that is

Utilizing babyparse (PapaParse) in nodejs to convert CSV to JavaScript objects has been quite challenging for me. After processing, the output of one object looks like this: { 'ProductName': 'Nike t-shirt', ProductPrice: '14.9 ...

What could be the reason for Sequelize to completely replace the record when updating in a put request?

I've been attempting to implement an "edit" feature within my project, but I've hit a roadblock in the process. Here's a snippet of the put request code: export const updateEvent = (event, id) => (dispatch, getState) => { request ...

I am unable to load any HTML files in my VueJS iframe, except for the index.html located in the public

Within the template of my vue component, I am utilizing the following code: <iframe width="1000vw" height="600vh" src="../../public/myHtmlFile.html"></iframe> Unfortunately, the file specified in the src attribut ...

The zoom level on Google Maps adjusts based on the size of the window when it

In reference to my previous inquiry about Google maps responsive resize, I am now looking to incorporate dynamic zoom levels based on the window size. Specifically, I want the map to automatically adjust its zoom level when the browser window is resized -- ...

Sturdy and adaptable in a container designed with a responsive width

Currently, I am developing a responsive app and I am facing the challenge of making some parts of the search bar responsive while keeping others fixed in width (like the search icon). I attempted to achieve this using the following code: .wrapper{ ...

Can regex matching characters be made easier to understand?

I am working on creating an ECMAScript (JavaScript) flavored regex to evaluate the complexity of my password based on specific criteria: Characters Used Password Strength Length ABC abc 123 #$& WEAK ... 1 x ...

Javascript Encapsulation example

Could someone help me with this function query: var MyObject3 = function (a, b) { var obj = { myA : a, myB : b } ; obj.foo = function () { return obj.myA + obj.myB ; } ; obj.bar = function (c) { return obj.myA + c ; } ; return obj ; } ; I und ...