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

Tips on showing content while filtering is taking longer with AngularJS

When working in Angular, I encountered a situation where filtering a large amount of data in a table was slowing down the process. To address this issue, I wanted to display a spinner every time a filter operation was in progress. Here is an example simil ...

Looking to optimize the JavaScript code for improved performance speed

Is there a more efficient way to avoid writing the same lines of code repeatedly without compromising performance? I've attempted using a for loop to categorize fields as 'mandatory' or 'optional', but it still requires duplicating ...

Use vanilla JavaScript to send an AJAX request to a Django view

I'm attempting to make a GET AJAX request to a Django view using vanilla JS. Despite passing is_ajax(), I am having trouble properly retrieving the request object. Below is my JavaScript code. Whether with or without JSON.stringify(data), it does not ...

image in responsive css design

Image 1 (Current Layout) Image 2 (Desired Layout) I am trying to achieve a responsive layout similar to the second image. However, I seem to be stuck with the layout from the first image. Here is the CSS code I'm currently using: @media (max-width ...

Angular and JS do not have the functionality to toggle the split button

I have a question that seems similar to others I've seen, but I haven't found a solution yet. Can someone please review my code? In the component, I have {{$ctrl.init}} and {{$ctrl.people}} assigned. I am sure this assignment is correct because ...

Ways to provide information to an rxjs observer

It appears that most people find observers to be a piece of cake, but I personally struggle with them. I am trying to set up an observable that can receive a number input after it has been created, triggered by pressing a button. However, all the examples ...

After transitioning my Image Modal from an ID to a Class, it appears to not be functioning properly

I recently implemented an image modal on my website using the ID attribute, but it didn't seem to work as expected. After changing the id to a class, now the image modal is not functioning at all and I'm struggling to figure out what went wrong. ...

Unable to modify the .top attribute style in elements within an arraylist using JavaScript

I need to align multiple images in DIVs on the same line by setting their Top value to match the first image in the array. Here is the code I am struggling with: (addBorder function is called when divs are clicked) <div class="DRAGGABLE ui-widget-cont ...

Error: Cannot access 'addEventListener' property of null in Chrome Extension due to TypeError

I've been working on developing a chrome extension that autofills input forms in web pages. However, I encountered an error that says "cannot read properties of null." This issue arises when I try to add an event listener to a button in my code. The f ...

Exploring AngularJS: the power of directives and the art of dependency

According to Angular documentation, the recommended way to add a dependency is by following these steps: Source //inject directives and services. var app = angular.module('fileUpload', ['ngFileUpload']); app.controller('MyCtrl&ap ...

Oops! An error occurred in AngularJs: "TypeError: $scope.todos.push is not a

I am currently facing an issue while using the $http.get method to add a todo from my controller to my database. The error message "TypeError: $scope.todos.push is not a function" keeps appearing, despite trying various solutions suggested by similar quest ...

I am looking to concatenate the existing URL with the language segment using jQuery

Looking to update the URL when clicking on a language name? Simply add the current path after the language section. For example, change the URL https://example.com/de_DE/about-us/ to https://example.com/us_EN/about-us/ and vice versa. I attempted this code ...

Encountered a problem while establishing a connection to Firebase following the upgrade of

Ever since I upgraded angularfire to version 2.0.1 in order to support the latest Firebase version 3.2.0, my app has been unable to establish a connection with the database. I'm not sure what could be causing this issue. Here's the code that used ...

CSS not working when attempting to override Angular (13) Material file

In my Angular (13) application with Material, I have developed a CSS file specifically for overriding certain Material styles. This CSS file is imported into the styles.scss file as the last line. However, despite importing the external file, the CSS def ...

Adjust the width of the dropdown menu for the v-combobox

I am currently working on creating a multiple search filter similar to the one found in Gitlab dashboard. For this task, I am utilizing Vuetify's v-combobox like this : <v-combobox id="mycombo" v-model="model&qu ...

Tips on how to make a <li> element float independently without impacting other elements on the page

I have a dilemma with two divs in my HTML code. One is designated for the menu, and the other is simply for the title. The issue arises when I attempt to float the <li> items next to each other within the menu; they end up shifting downwards from the ...

Extract specific elements from an array using Mongoose's $slice operator while still maintaining the

Currently, my task is to retrieve the total number of items in my News object and return a portion of those items as objects. While I have successfully implemented the $slice operator in my query, I am struggling to determine the original array size of the ...

What is the best way to keep an image fixed at the bottom, but only when it's out of view in the section

There are two buttons (images with anchors) on the page: "Download from Google Play" and "Download from App Store". The request is to have them stick to the bottom, but once the footer is reached they should return to their original position. Here are two ...

Tips for updating and transferring a variable within a callback function

I have implemented a function using the SoundCloud API that retrieves a song URL, obtains the associated sound ID from the API, and then passes that ID to a callback for streaming purposes and updating the page. The data is successfully retrieved from the ...

Parent container fails to center align when window is resized

When I resize my window to a smaller size, like that of a smartphone screen, the three inner divs (colored red, green, and blue - .left, .center, and .right) do not align in the center. Please refer to the screenshot attached for clarity. My goal is to ens ...