Implementing a bi-directional animation transition with the ng-animation-swap directive

I am seeking a unique slider-like feature, similar to a PowerPoint presentation where clicking different buttons yields various outcomes.

Using ng-animate-swap effectively allows me to switch between displayed slides smoothly, but I encounter an issue when attempting to navigate in reverse. While I can change the animation class, the soon-to-be previous slide retains the previous animation state. You may view a functional example inspired by the ng-animate-swap documentation here: http://plnkr.co/edit/dArF1W7eM7Znur7Myx3O?p=preview

The problem lies in the fact that CSS class changes are only applied to the new slide and not to the one being removed.

Below is the relevant code :

index.html :

<body ng-app="ngAnimateSwapExample">
  <div ng-controller="AppCtrl">
     <div class="container">
       <div ng-animate-swap="number" class="cell {{swapAnimation}}" ng-class="colorClass(number)">
         {{ number }}
       </div>
    </div>
    <a ng-click="previousNumber()">PREVIOUS</a>
    <a ng-click="nextNumber()">NEXT</a>
  </div>
</body>

script.js :

.controller('AppCtrl', ['$scope',function($scope) {
$scope.number = 0;

var colors = ['red','blue','green','yellow','orange'];
$scope.colorClass = function(number) {
  return colors[number % colors.length];
};

$scope.nextNumber = function(){
  $scope.swapAnimation = "swap-animation";
  $scope.number += 1;
};

$scope.previousNumber = function(){
  $scope.swapAnimation = "swap-animation-reverse";
  $scope.number -= 1;
};


}]);

animations.css :

.container {
   height:250px;
   width:250px;
   position:relative;
   overflow:hidden;
   border:2px solid black;
 }
.container .cell {
   font-size:150px;
   text-align:center;
   line-height:250px;
   position:absolute;
   top:0;
   left:0;
   right:0;
   border-bottom:2px solid black;
}
.swap-animation.ng-enter, .swap-animation.ng-leave {
   transition:0.5s linear all;
 }
 .swap-animation.ng-enter {
   top:-250px;
 }
 .swap-animation.ng-enter-active {
   top:0px;
 }
 .swap-animation.ng-leave {
   top:0px;
 }
 .swap-animation.ng-leave-active {
   top:250px;
 }


 .swap-animation-reverse.ng-enter, .swap-animation-reverse.ng-leave {
   transition:0.5s linear all;
 }

 .swap-animation-reverse.ng-enter {
   top:250px;
 }
 .swap-animation-reverse.ng-enter-active {
   top:0px;
 }
 .swap-animation-reverse.ng-leave {
   top:0px;
 }
 .swap-animation-reverse.ng-leave-active {
   top:-250px;
 }

Answer №1

To achieve smoother animations, consider adding a timeout after changing the cssClass within your nextNumber() and previousNumber() logic. By doing this, the element will change classes in one cycle and then execute ng-animate-swap animation in the next cycle.

$scope.nextNumber = function(){
  $scope.swapAnimation = "swap-animation";
  $timeout(function(){
    $scope.number += 1;
  });
};

$scope.previousNumber = function(){
  $scope.swapAnimation = "swap-animation-reverse";
  $timeout(function(){
    $scope.number -= 1;
  });
};

http://plnkr.co/edit/yEDmEWRGxIrAnalQF20u?p=preview

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

Leverage PHP variables within AJAX requests

I am currently working on implementing a specific functionality on the page "videos.php" (please note that this is all contained within a PHP echo statement): First, when a user clicks .star_' . $pvid_ID . ', it triggers the submission of a vid ...

selectize.js typescript: Unable to access values of an undefined object (reading '0')

I've been working on incorporating selectize.js into my project using webpack and typescript. After installing selectize.js and the necessary types, I added the following to my code: yarn add @selectize/selectize yarn add @types/select2 Within my c ...

"Troubleshooting AngularJS UI Bootstrap Typeahead: How to Fix the Issue

Here is my HTML code snippet: <p> <input type="text" ng-model="destination" placeholder="Destination" uib-typeahead="dest as dest.name for dest in fetchDestinations($viewValue)" typeahead-loading="loadingDestinations" typeahea ...

What is the best way to declare the data type of an endless generator?

Below is an example of JavaScript code: /** * Generates a sequence of numbers. * * @param {number} i - The starting number. * @yields {number} The next number. */ function* gen(i) { while (true) { yield i++; } } const g = gen(1); // Case 1 ...

Utilize NodeJS API to convert a base64 data string into a downloadable PDF file

My NodeJS API is set up to communicate with another API and fetch a data object in the form of a Base64 string. The client making the API call needs to be able to download a PDF file generated from this base64 data. What is the best way to return the dat ...

Using JavaScript to execute a JSON parse function will not work

I am currently working on this code and would appreciate any assistance. I'm trying to retrieve and parse data from my listApp.json file to display a list with one link. As a beginner, I could use some guidance. <script type = "text/javascript"> ...

How can I preserve the file extension of an ejs file as .html?

I'm in the process of building an expressjs application using the ejs template engine. However, I'd like to retain the file extension as .html instead of using .ejs. The main reason for this is that I am using Visual Studio for my development wor ...

Arrangement featuring two distinct types of tiles

Looking for a way to achieve this layout using just CSS or a combination of CSS and a little JS. Click on my avatar to see the reference image enlarged =) I've tried using floats and display options but haven't been successful. Take a look at htt ...

Firestore storage calls are leading to app freeze

My code seems to be looping through the images but the console.log output is overwhelming. There are only 3 images in each folder - one named back and the other named front. How can I display all images without causing the app to slow down? Remember, there ...

The error message that occurs when using angularjs $scope.$apply() is: Error: [$rootScope:inprog]

Having trouble updating text on a page within the $scope. Facing this error message: Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply] at Error (native) at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/ ...

Update the Vue component upon fetching new data

How can I continuously refresh the list of items when a button in a sibling component is clicked? The watch method only triggers once, but I need it to constantly refresh. This is the parent element: <template> <div class="container"& ...

Accessing geolocation on a mobile web browser: A step-by-step guide

I have implemented HTML5 geolocation functionality using code I found in the documentation: getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(this.showPosition, this.showError); } else { alert("Geoloc ...

Guide on sending a JavaScript variable to PHP using AJAX

I am currently utilizing the following JavaScript code to obtain data from a td element when a button is clicked within an onclick event. function rfk(element) { var element = element.parentElement.parentElement; var id = parseInt(element.childre ...

Apexcharts tends to be unreliable upon dashboard loading, frequently appearing and disappearing unpredictably

Having trouble with displaying apexcharts on my dashboard. There are 5 charts on the dashboard and their behavior is acting strange. The charts only show up after reloading the page 3 to 4 times or when inspect element is clicked. Otherwise, only 2 or 3 ch ...

Creating a dropdown feature in the sidebar menu of the Ionic framework

Discovering the ins and outs of the ionic framework is a new adventure for me. Currently, my focus lies on creating a nested drop-down element within a side menu that I've successfully implemented. Clicking on an item like "act&ser" reveals its li ...

Applying CSS to a jQuery variable: Step-by-Step Guide

$(document).ready(function() { var today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()); $('#startdate').datepicker({ uiLibrary: 'bootstrap4', iconsLibrary: 'fontawesome', ...

Ways to access the previous and following elements that belong to a specific class, but are not directly related

Below is the code snippet provided: <ul> <li class="active"> <div class="course_video_heading"><span class="minus"></span> Introduction <div class="course_duration" align="right">1m 21s</div></div&g ...

Integrating Amazon external images in NextJS

There is a specific issue with loading images from a URL stored on Amazon S3 within the domain configured in next.config.js. Strangely, when using external URLs like Unsplash, the images load fine. The problematic URL is: idinheiro-admin-images.s3.sa-east ...

Creating a virtual store for cryptocurrencies using MongoDB and Discord.js

Currently working on developing an adventure-themed Discord bot. I've successfully programmed numerous commands and even implemented a currency system, allowing users to earn money. However, I'm facing a challenge in creating a store where player ...

What could be the reason for sending XmlHttpRequest even if ClientValidation has failed?

Having multiple AJAX forms on my page poses a challenge when I need to submit them all on button click. Simply iterating over the forms with forms.each(function (index, form) { $(form).submit();} won't work as expected because only the last form gets ...