Angular animations do not seem to be functioning properly with ng-show when changing the class using ng-class

I need assistance in creating a slider for a list of objects using ng-show and animations. The current setup works smoothly when the objects slide in one direction.

However, I am facing issues when trying to implement the functionality for users to slide the objects left or right by changing classes with ng-class. This is causing the animations to fail.

Below is the HTML code for handling the left/right sliding:

<div class="slide-holder">
  <ul class="slide-list">
    <li class="slide-object" ng-show="directionElement == 0" ng-class="{'slide-object-left': direction === 'left', 'slide-object-right': direction === 'right'}">
      Hello There 1! How are you?</li>
    <li class="slide-object" ng-show="directionElement == 1" ng-class="{'slide-object-left': direction === 'left', 'slide-object-right': direction === 'right'}">
      Hello There 2! How are you?</li>
    <li class="slide-object" ng-show="directionElement == 2" ng-class="{'slide-object-left': direction === 'left', 'slide-object-right': direction === 'right'}">
      Hello There 3! How are you?</li>
    <li class="slide-object" ng-show="directionElement == 3" ng-class="{'slide-object-left': direction === 'left', 'slide-object-right': direction === 'right'}">
      Hello There 4! How are you?</li>
  </ul>
</div>

The controller functions responsible for changing the direction are as follows:

$scope.left = function() {
  $scope.direction === 'left'
  if($scope.directionElement > 0)
    $scope.directionElement = $scope.directionElement - 1;
};

$scope.right = function() {
  $scope.direction === 'right'
  if($scope.directionElement < 3)
  $scope.directionElement = $scope.directionElement + 1;
};

Here is the CSS code for transitions:

.slide-object-left.ng-hide-add,
.slide-object-left.ng-hide-remove {
  -webkit-transition:0.5s linear all;
  -moz-transition:0.5s linear all;
  -o-transition:0.5s linear all;
  transition:0.5s linear all;

  position:absolute;
}

.slide-object-left.ng-hide-add {
  left:100%;
}

.slide-object-left.ng-hide-remove,
.slide-object-left.ng-hide-add.ng-hide-add-active {
  left:0;
}

.slide-object-left.ng-hide-remove.ng-hide-remove-active {
  left:-100%;
}

To view both cases in action, I have created a plnkr demonstration: http://plnkr.co/edit/sh0uCAPZiCne4Y5ynFQ2?p=preview

UPDATE 1: I have rectified the '===' error in the controller which caused the direction switching problem, based on the feedback from Theoretisch. Nevertheless, the main issue with ng-class and animations persists. Here is the updated plnkr link for reference: http://plnkr.co/edit/lv1BBFjRoOmenTv7IBeC?p=preview

Answer №1

The issue causing the animation to not work is due to the use of === in your controller functions.

Instead of ===, you should use just = as you are not comparing $scope.direction with a string.

$scope.left = function() {
  $scope.direction = 'left'
  if($scope.directionElement > 0)
    $scope.directionElement = $scope.directionElement - 1;
};

$scope.right = function() {
  $scope.direction = 'right'
  if($scope.directionElement < 3)
    $scope.directionElement = $scope.directionElement + 1;
};

With this change, the animation should work properly once again. However, there are additional adjustments needed for a smooth and accurate animation. For example, modifying your CSS can help enhance the animation quality.

To correct this, make the following updates:

.slide-object-left.ng-hide-add {
  right:-100%;
}

.slide-object-left.ng-hide-remove,
.slide-object-left.ng-hide-add.ng-hide-add-active {
  right:0;
}

.slide-object-left.ng-hide-remove.ng-hide-remove-active {
  right:100%;
}

.slide-object-right.ng-hide-add {
  left:-100%;
}

.slide-object-right.ng-hide-remove,
.slide-object-right.ng-hide-add.ng-hide-add-active {
  left:0%;
}

.slide-object-right.ng-hide-remove.ng-hide-remove-active {
  left:100%;
}

I have changed right to left and adjusted the signs accordingly. You can view the updated Plunk with my modifications HERE.

EDIT: The animation may be glitchy due to the use of ng-class. I removed it and made edits to your ng-show. Check out the revised Plunk HERE. It's not a perfect solution, but hopefully it resolves your issue for now. (You could potentially enhance it using THIS fiddle)

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

Align three divs with three columns in the horizontal center

I am facing a challenge where I need to perfectly center 3 col3 divs in a row. Despite the calculation showing 1.5, I am unsure of the proper method to overcome this hurdle. <div class="row"> <div class="col-md-offset-2 col-md-3" style="height: ...

One particular Django template is failing to load the CSS, while the rest of the templates are

In my folder, I have two Django templates. One template is for the URL localhost:8000/people and it correctly fetches CSS from /m/css/style.css. The problem arises with the second template meant for the URL localhost:8000/people/some-name. This template f ...

Activate on click using JavaScript

When a link with the class .active is clicked, I want to use a JavaScript function to deactivate any other active links. The structure of the code should be as follows: <ul class="product"> <li><a href="#myanmar" class="active">Mya ...

How to use JQuery to retrieve multiple background image URLs from CSS styling

Initially, here is the CSS code that I am working with: background-image: url(/style_elements/img/first.png), url(/style_elements/img/second.png); I am trying to specifically target the second background image URL using jQuery: var item_img_url = item_i ...

Hide jquery scroll bar

I am currently working on a WordPress plugin with the Twenty Thirteen theme. My goal is to display a modal when a div is clicked, and at that time I want to hide the scrollbar on the body of the page. Despite trying the following code snippet, it doesn&ap ...

What is the best way to generate an extensive table with HTML and AngularJS?

I am looking to implement an expandable table feature where a single header with a plus icon can be clicked to reveal the child rows beneath it. Currently, I have been able to achieve this functionality. However, I am facing an issue where the child rows ...

Utilize strings as variables within SASS code

In my project, I have defined two variables called $person-color and $animal-color in a separate file named variables.scss. Now, I want to use these variables in my main stylesheet main.scss. The desired outcome is to have the following CSS styles applied ...

Switch out the rowspan attribute in HTML for a CSS alternative

Hello there, I've been experimenting with replacing my table layout with divs and CSS, but I'm struggling to find a way to replicate the behavior of the rowspan attribute. Here is the original code snippet: <table> <tr> <td> ...

Recalling the layout following the user's computer restart

I am creating a simple online editing tool (similar to Microsoft Outline) for coursework and I would like the outline details to be saved even if the user restarts the system. How can I achieve this? <html> <head> <title>Editor</ ...

Challenges with unique scrollbar designs and dynamic tab loading using AJAX

Hello to all amazing members of stackoverflow, I'm facing some major challenges getting my custom scrollbar to function properly with ajax-based tabs. Any assistance you can provide would be immensely helpful. I have most of it set up and operational ...

Angular 8 is facing an issue where classes defined dynamically in the 'host' property of a directive are not being properly applied to the parent template

In my Angular 8 application, I am working on implementing sorting using the ng-bootstrap table. I referred to the example available at . In the sample, when I hover over the table header, it changes to a hand pointer with a highlighted class applied as sho ...

Integrate Javascript code into a JavaScript file

Is there a way to incorporate a JavaScript link inside a .js file? Does a .js file support include, require, or import similar to CSS or PHP? Here is the code in question: // Global site tag (gtag.js) - Google Analytics var script = document.createElemen ...

"Implementing a feature to apply the 'current' class to a div

How can I dynamically add a current class to the .navimg divs (next to li) when their parent link is clicked? Sample HTML: <div id="navbox"> <ul id="navigation"> <li id="home"><a href="#">HOME</a></li> ...

The persistent space between the navbar and the index page remains despite the suggested fixes provided

There appears to be a discrepancy between the navigation bar and the rest of the page. Despite my efforts to adjust margins, I haven't been able to resolve it. The system is prompting me for more details before posting this question, but I'm unsu ...

Troubleshooting issues with AngularJS $watch not triggering properly

Even though Deep watch has been activated in the factory, it is not triggering. What steps can be taken to resolve this issue and ensure that an event is triggered when the 'name' value changes? Javascript Code: var app = angular.module('a ...

The foundation gem is encountering an issue with incompatible units: 'rem' and 'px'

After installing the foundation gem version 5.5.2.1, I encountered an error when starting the application: Showing /home/user/Scrivania/sites/store/app/views/layouts/application.html.erb where line #9 raised: Incompatible units: 'rem' and &apos ...

access older siblings, accordion animation

I need assistance achieving an accordion effect on a DIV when hovering over it. The right side of the accordion is functioning properly, but I am facing issues with the left side. You can view my code on jsFiddle Could someone please provide guidance on ...

Restricting data list values in AngularJS

Currently, I am facing a performance issue while trying to display approximately 2000 values retrieved through an API call using DataList in AngularJS. The page becomes extremely slow due to the large number of items being rendered. Is there a way to optim ...

Retrieving data from a nested object with varying key names through ng-repeat

My JSON object contains various properties with unique names: var definitions = { foo: { bar: {abc: '123'}, baz: 'def' }, qux: { broom: 'mop', earth: { tree: 'leaf', water: 'fi ...

Develop an engaging billboard carousel using jQuery

After coming across a tutorial on tympanus, I decided to make some modifications to create a rotating billboard effect. However, something seems to be going wrong and it's driving me crazy trying to figure out the problem! $(function() { $('#ad_ ...