Animation effects for lists with AngularJS and CSS styling

I'm struggling with understanding basic AngularJS 1.4+CSS animation concepts.

I have a list where new items are added at the top and removed from the bottom when the maximum number of items (5 in the example below) is reached.

The code below achieves a fade animation for this situation:

angular.module("testApp", ["ngAnimate"])
  .controller("testCtrl", function($scope) {
    var lastId = 0;
    $scope.items = [];
    $scope.add = function() {
      if (!$scope.newValue) { return; }
      $scope.items.unshift({id: ++lastId, name:$scope.newValue});
      $scope.newValue = '';
      while ($scope.items.length > 5) {
        $scope.items.pop();
      }
    };
  });
.list-item.ng-enter {
  transition: all 0.4s ease-out;
  opacity: 0;
}
.list-item.ng-enter.ng-enter-active {
  opacity: 1;
}
.list-item.ng-leave {
  transition: all 0.4s ease-out;
  opacity: 1;
}
.list-item.ng-leave.ng-leave-active {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-animate.min.js"></script>
<form ng-app="testApp" ng-controller="testCtrl">
  <div>
    <input type="text">
    <button ng-click="add()">Add</button>
  </div>
  <table>
    <tr class="list-item" ng-repeat="item in items track by item.id">
      <td>{{item.name}}</td>
    </tr>
  </table>
</form>

However, I am looking for animations where the newly added items expand in height as they slide in from the top, the exiting items shrink in height as they slide out from the bottom, and the remaining items move downwards to their new positions. I'm unsure how to achieve this effect.

I attempted using the height property instead of opacity, but it did not work well. I also want the elements to resize based on font size rather than setting an explicit height.

The preferred method is changing the height, but it would also be acceptable to have a pure sliding animation with fading effects. It's crucial that the animations do not affect the size of the parent or surrounding elements on the page.

Usually, only one item enters or leaves at a time, although there may be scenarios where two items enter or leave simultaneously. Ideally, the animations should delay appropriately to give the appearance of sequential sliding, but complexity might prevent this.

In a previous version of the page, jQuery animations were used with "slideUp" for removal and "slideDown" for addition, maintaining order since items were manually inserted/deleted before Angular. This effect worked well, and I aim to replicate it through CSS animations or ng-repeat, though I'm uncertain if they support controlling animation order like jQuery did.

Answer №1

After some diligent testing, it appears that the modified code snippet below is functioning correctly.

Notable modifications include:

  • It seems that tables do not animate smoothly, so using <li> elements instead produces better results.
  • The use of overflow-y: hidden alongside animating max-height is necessary for proper functionality.

angular.module("testApp", ["ngAnimate"])
  .controller("testCtrl", function($scope) {
    var lastId = 0;
    $scope.items = [];
    $scope.add = function() {
      if (!$scope.newValue) { return; }
      $scope.items.unshift({id: ++lastId, name:$scope.newValue});
      $scope.newValue = '';
      while ($scope.items.length > 5) {
        $scope.items.pop();
      }
    };
  });
.list-item.ng-enter {
  transition: all 0.5s linear;
  max-height: 0;
  overflow-y: hidden;
}
.list-item.ng-enter.ng-enter-active {
  max-height: 20px;
}
.list-item.ng-leave {
  transition: all 0.5s linear;
  max-height: 20px;
  overflow-y: hidden;
}
.list-item.ng-leave.ng-leave-active {
  max-height: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-animate.min.js"></script>
<form ng-app="testApp" ng-controller="testCtrl">
  <div>
    <input type="text" ng-model="newValue" />
    <button ng-click="add()">Add</button>
  </div>
  <ul>
    <li class="list-item" ng-repeat="item in items track by item.id">
      {{item.name}}
    </li>
  </ul>
</form>

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

Having trouble getting the overflow scrollbar to work properly?

I recently created a Whiteboard using Vue. The Whiteboard consists of an SVG element where I can add other SVG elements like paths to it. In order to enable scrolling within the SVG, I came across this example which seemed quite helpful. Check out the exa ...

What is the best way to make this CSS arrow see-through?

My goal is to achieve this desired outcome: https://i.sstatic.net/4eTpE.png Currently, I have the following (focusing only on the left element for now): https://i.sstatic.net/nUFp1.png I am struggling to make the left arrow transparent and I can't ...

Ways to eliminate the gap produced by a fixed element

Why is my chatbox (marked in red) creating unnecessary space at the top (highlighted in yellow), even though it has a sticky position and should not be affecting that area? The other elements on the webpage seem to be impacted by this. How can I prevent th ...

JavaScript Time Counter: Keeping Track of Time Dynamically

Currently, I am attempting to create a dynamic time counter/timer using JavaScript. Why dynamic? Well, my goal is to have the ability to display days/hours/minutes/seconds depending on the size of the timestamp provided. If the timestamp is less than a d ...

Positioning three divs next to each other and aligning them at the bottom of the longest div

While I've seen similar questions about aligning divs in a row, my situation has a slightly different twist. I have three divs that I want aligned horizontally, with their content pushed to the bottom. I've attempted using position: relative and ...

Arrange items in a row in the navigation bar

I am currently working on aligning my navbar items instead of having them stack. After switching from Bootstrap 3 to Bootstrap 4 (beta 2), I noticed that they no longer align properly and remain stacked. Below is the code snippet: <!doctype html> ...

The alignment of Font Awesome Icons within a Bulma menu is not as expected

Is there a way to properly align font awesome icons with text in a Bulma menu without the need for custom CSS? Icons align correctly in a Bulma navbar item, but appear misaligned in a Bulma menu. https://i.sstatic.net/EpmSe.png <!DOCTYPE html> ...

The database has unfortunately registered an incorrect value for the "date" input after it was saved

When I select a date from the datepicker input field, it is being incorrectly saved in the database. I am selecting the date using the datepicker and then using AngularJS to send it to Spring MVC AngularJS: $scope.updateProjectDetails = function(detail) ...

binding swipe action to click event

I am utilizing images from a database and implementing next and previous buttons for pagination. What I aim to do is connect the click events of these buttons to swipe actions, so that swiping from right to left triggers the next link, while swiping from l ...

What is the best approach to assigning a default value to @Html.EditorFor(model => model.location)?

After attempting to add , new { @Value = "test" } and failing, I then made modifications to the model as follows: private string _location = string.Empty; public string location { get { return _location; } set { _location = value; } } Even when ...

What steps are involved in updating the dataSource and refreshing a kendo grid with fresh data?

In the Add owner grid configuration, I have a function called $scope.addProcessOwner. When a user clicks on each owner, a new array object is created with the data stored in selectedOwners. Now, I want to set selectedOwners as the dataSource of the selecte ...

In Internet Explorer, when utilizing a table-cell with a variable height, the position should be set to absolute for

I'm struggling with getting a uniform height in my horizontal layout using display: table-cell. While it looks great in Chrome, I can't seem to get Internet Explorer to display it the same way. Check out this link for reference This is how I wa ...

AngularJS: Error: Attempting to access property 'get' of an undefined object

I encountered an issue while attempting to utilize a service within my angular controller. The error message I received is TypeError: Cannot read property 'get' of undefined. This is the structure of my app.js script: angular .module(' ...

Adding the ng-app name does not yield the anticipated results when using the Application

Struggling to include the ng-app tag in my AngularJS application example. Each time I attempt to specify a name like this ng-app="myapplication", the webpage ceases to function. It should display the entered name after the greeting "Hello" as demonstrated ...

Establishing a connection between Python and MySQL on a Windows operating system using

Here is the source code I am working with: import pymysql import socket conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='user', passwd=None, db='extractor') cur = conn.cursor() cur.execu ...

Decoding HTML with PHP

I'm facing an issue with creating a Web page Parser. The structure of the page is as follows: <TABLE WIDTH=80%> <tr><td colspan=7><BR><BR></td></tr> <TR> <Td colspan=7><FONT FACE="arial" align ...

Tips for creating zoomed-in background images on small screens

I am currently implementing this code for a website project .homepage{ background-image: url(/wp-content/themes/ep/img/_.png); -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background ...

Using AngularJS ngRepeat orderBy functionality in the Chrome browser

I have developed a basic Angular app that displays rows of data. I want the rows to maintain the order in which they were added to the rows array when the controller is initially loaded with data. To achieve this, the orderBy property starts off as an empt ...

Styles in print CSS are not effective in an Angular project

Currently, I am working on an Angular project where I need to generate a printable document using CSS. The challenge I am facing is ensuring that the date and title in the header do not print automatically when the document spans multiple pages. Additional ...

Sculpting the excess from a div-generated HTML button

I need help with adjusting the appearance of a link to make it look like a button. Currently, the button looks too "fat" because of the red background around the text. How can I reduce this effect? I've tried tweaking the width of the div, but that j ...