Can ngAnimate facilitate conditional transitions?

In my application, I have set up an animation using ngAnimate on ngView where the next view slides in from the right every time there is a state change.

Now, I want to be able to reverse this behavior so that sometimes the view slides in from the left (for instance, when the user is navigating back). However, since the only available classes are .ng-enter, .ng-leave, and .*-active, I am unsure of what options I have. Is it even possible?

You can view a demo on CodePen, OR check out the sample code below:

HTML

<div ng-app="app">
  <div ng-controller="DemoController as vm">
    <div ui-view class="foo"></div>
  </div>
</div>

CSS

html,
[ui-view] {
 background: yellow; 
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  position: relative;
}

div {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  width: 100%;
}

[ui-view].ng-enter,
[ui-view].ng-leave {
  position: absolute;
  left: 0;
  right: 0;
    transition:all .5s ease-in-out;
}

[ui-view].ng-enter {
  opacity: 0;
  transform:translate3d(100%, 0, 0);
  /*transform:scale3d(0.5, 0.5, 0.5);*/
}

[ui-view].ng-enter-active {
  opacity: 1;
  transform:translate3d(0, 0, 0);
  /*transform:scale3d(1, 1, 1);*/
}

[ui-view].ng-leave {
  opacity: 1;
  transform:translate3d(0, 0, 0);
}

[ui-view].ng-leave-active {
  opacity: 0;
  transform:translate3d(-100%, 0, 0);
}

Javascript

'use strict';

angular.module('app', ['ui.router', 'ngAnimate'])
.config(function ($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('/one');  
  $stateProvider
    .state('one', {
      url: '/one',
      template: '<div class="one"><h1>ONE!</h1><button ng-click="vm.two()">go to two</button></div>'
    })
    .state('two', {
      url: '/two',
      template: '<div class="two"><h1>TWO!</h1><button ng-click="vm.one()">go to one</button></div>'
    });;
})
.controller('DemoController', DemoController);

DemoController.$inject = ['$state'];
function DemoController($state) {
  this.one = function()  {
    $state.go('one');
  }

  this.two = function() {
    $state.go('two');
  }
}

Answer №1

While I won't provide the actual code for this task, I can offer a suggested approach.

You'll need to monitor the history using $stateChangeStart and compare the current path with the next path.

Based on whether there is a match or not, you can toggle a class on ui-view.

Afterward, create a set of CSS rules that target this specific class.

[ui-view].ng-enter.fromRightClass{}

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

Header Stability TransitionAndView

I'm not very experienced with JavaScript, so please bear with me. I'm attempting to create a fixed header that transitions to 50% opacity when scrolled down and returns to full opacity (opacity: 1.0) when scrolled back to the top. Here is my cur ...

The responsive table works flawlessly in all platforms except for the default iOS 7 mail application

When using the default iOS 7 (iOS 7.1.1) mail app, there seems to be an issue with rendering a table correctly. Browsers like Chrome, Firefox, and Safari (latest versions) display both the desktop and mobile views perfectly. However, Internet Explorer only ...

Prevent floating labels from reverting to their initial position

Issue with Form Labels I am currently in the process of creating a login form that utilizes labels as placeholders. The reason for this choice is because the labels will need to be translated and our JavaScript cannot target the placeholder text or our de ...

Text box size in user input not adapting on mobile devices

Website: Visit the Cutter Travel Calculator here I've encountered an issue with my user input text boxes on mobile. Despite setting their width using the formidable forms plugin, the size adjustment doesn't seem to apply when viewed on mobile de ...

Tips for adding CSS styling to the action button within the ShinyWidgets package in Shiny applications

I have created a shiny app example below. In order to align the actionButton with selectInput, I had to include style='margin-top:25px'. The Shinywidgets package offers actionBttn widgets with predefined styles. For instance, I prefer the one wit ...

Button for expanding Google Maps API v3

My Ionic app using Google Maps v3 API recently added a new button that expands the map, but I can't figure out how to remove it. Has anyone encountered this issue before? If you need a visual reference, here is a screenshot showing the button at the ...

Use a for loop in html, css, and javascript to generate unique element ids with corresponding values

Can you assist me in creating a function to generate element_id-value pairs and saving them into a file with the use of a for loop? <body> <form class="box1" > <fieldset> <label class="my_text4" >firstelement&l ...

Anguar server encountered a startup issue and failed to initialize

My server.js file looks like this: var express = require('express'), api = require('./api'), app = express(); app .use(express.static('./public')) .use('./api', api) .get('*', ...

Issues with CSS selectors in Internet Explorer causing problems with Selenium Webdriver C#

When automating a website with Selenium C#, I encountered a NoSuchElementException when trying to click an element using a CSS selector. However, the issue was resolved when using xpath instead. Can someone shed light on why this discrepancy between CSS an ...

After a page refresh, the jQuery slideDown effect abruptly stops

I've posed this question before without receiving any responses, so I'm giving it another shot. In WordPress, I crafted a navigation menu that utilizes jQuery for a slide down/up animation on submenus. The issue arises when using flexbox on the ...

Adjusting the transparency of the background without altering its colors

I need help figuring out how to merge custom palettes I've created such as this one, which resulted in a .css and .less file. While I want to retain the colors, I also require certain elements' backgrounds to be transparent. To achieve this, I c ...

What is the best way to break down a <table> into several <div> elements?

Currently, I am dealing with a vast amount of data presented in a table format with numerous rows and columns. To manage this, I have opted to enclose the table within an overflow:scroll div. However, I am seeking a solution that allows me to keep the firs ...

The div is set to a position of absolute, causing the overflow-y property to be set to auto. As a

I am facing an issue with a div that has absolute positioning nested inside other divs. If you take a look at the code snippet below from http://jsfiddle.net/d8GYk/, you'll notice the styling properties applied to the div. <div style="position: ...

Can I position two div elements next to each other, with one div partially offscreen, without utilizing display none or setting its width to zero?

Is it possible to position one div outside of the screen width while setting two divs side by side? I am looking to insert DIV2 above DIV1 with animation without adjusting its width or display. The goal is for DIV2 to enter the screen from the side alread ...

Can you explain the variance between these two methods of configuring the $routeProvider?

As I delve into the world of Angular, I've come across various configurations for the $routeProvider. One example sets it up like this: app.config(function ($routeProvider) { $routeProvider. when("/drivers", { templateUrl: "partia ...

AngularJs Control Granularity

Coming from the Asp.net MVC world, I'm used to creating a single controller per model like this: public class FooController : Controller { public ActionResult Index() {...} public ActionResult Details(long id) {...} public ActionR ...

Step-by-step guide to implementing a floating action button on the bottom-right corner of a screen using Material-UI in a React application

I have been attempting to place the FloatingActionButton on the bottom right corner of the screen. To achieve this, I am utilizing the following library: http://www.material-ui.com/#/components/floating-action-button import React, { Component } from "reac ...

Is there a way to shift my navigation to the right while keeping the (display: inline;) property intact?

Is there a way to align my navbar to the right side while keeping the display: inline; property intact? When I remove the display: inline; property, the li elements automatically align to the right side of the page, but in a list format. HTML <div id= ...

Combining AngularJS 2 within an existing Symfony 2 project

We are currently in the process of transitioning an old Flex project to HTML5. In order to do this, we are creating a proof of concept in AngularJS2 and looking to integrate services with our existing PHP Symfony 2 backend. However, we are experiencing ses ...

AngularJs $resource HTTP status monitoring

As a newcomer to AngularJs, I am delving into the complexities of utilizing $resource to retrieve the HTTP status code that was returned. I have set up a factory and attempted one method to obtain the HTTP header but haven't had any success so far. a ...