Incorporating ng-animate with the dynamic animations of animate.css

I am currently exploring the functionality of ngAnimate.

In order to implement ngAnimate, I have included angular-animate.js, animate.css, and angular 1.3 in my project.

To activate ngAnimate, I added it to my app configuration and also included some CSS code like this:

.ng-leave{
   -webkit-animation-name: fadeInUp;
   -moz-animation-name: fadeInUp;
   -o-animation-name: fadeInUp;
   animation-name: fadeInUp;
}

This setup works because the fadeInUp animation is defined in animate.css.

Now, I want to achieve the same result without directly writing CSS code (just by using animate.css).

I attempted something similar to the following, but it did not work as expected:

<li class="list cars" ng-repeat="item in cars track by item.id" ng-animate="{enter: 'fadeInUp animated', leave: 'fadeOutUp animated'}">

Any suggestions on how to make this work?

Thank you

Answer №1

Steps to add animations in AngularJS:

1) Start by providing the necessary dependency:

angular.module('yo', [
        'ngAnimate'
    ]);

Make sure to include "angular-animate.min.js" in your script tag.

2) There are three ways to implement animation effects:- a)CSS Transitions. b) CSS Keyframe Animations c) JavaScript Animations.

3) Select any of the above methods, for example, if your template looks like this:

<div ng-init="on=true">
  <button ng-click="on=!on">Toggle On/Off</button>
  <div class="my-special-animation" ng-if="on">
    This content will enter and leave
  </div>
</div>

To animate using CSS Transition, simply add the required class attribute in your element and apply the following css:

.my-special-animation.ng-enter {
  -webkit-transition:0.5s linear all;
  transition:0.5s linear all;

  background:red;
}

.my-special-animation.ng-enter.ng-enter-active {
  background:blue;
}

Check out the plunker here: http://plnkr.co/edit/?p=preview

CSS Keyframe animations offer more control than Transitions and are supported by most browsers. Here's an example:

.my-special-animation.ng-enter {
  -webkit-animation:0.5s red-to-blue;
  animation:0.5s red-to-blue;
}

@keyframes red-to-blue {
  from { background:red; }
  to { background:blue; }
}

@-webkit-keyframes red-to-blue {
  from { background:red; }
  to { background:blue; }
}

Take a look at the Plunker demo: http://plnkr.co/edit/?p=preview

If you prefer JavaScript Animations for more advanced effects, you can define a factory-like function within your module code as shown below:

myApp.animation('.my-special-animation', function() {
  return {
    enter : function(element, done) {
      jQuery(element).css({
        color:'#FF0000'
      });

      jQuery(element).animate({
        color:'#0000FF'
      }, done);

      return function(cancelled) {
        if(cancelled) {
          jQuery(element).stop();
        }
      }
    },

    leave : function(element, done) { done(); },
    move : function(element, done) { done(); },

    beforeAddClass : function(element, className, done) { done(); },
    addClass : function(element, className, done) { done(); },

    beforeRemoveClass : function(element, className, done) { done(); },
    removeClass : function(element, className, done) { done(); },

    allowCancel : function(element, event, className) {}
  };
});

View the JavaScript Animation example on Plunker: http://plnkr.co/edit/?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

Angular: identifying inconsistencies between previous and current values with range slider and numeric input spinner

Attempting to resolve a discrepancy between oldVal and newVal in a 'range slider' and 'number spinner input' that are both linked. Discovered a bug where the difference between oldVal and newVal is much larger than expected, considering ...

Adjust the height and width dynamically in CSS/HTML based on various screen dimensions

There are 2 major concerns I have regarding this layout : .feature_content (with a grey background) does not adjust its height and width to different screen sizes. Currently, on large screens, .feature_content is positioned far from the footer. There is ...

tips for incorporating margin without sacrificing responsiveness

Having an issue with three cards in a row, they are too close together without any margin. It doesn't look good, so I tried adding margin, but it messes up the responsiveness that Bootstrap provides. So, I decided to add padding only. Any suggestions ...

What is the best way to make sure every HTML element on this page is perfectly centered on the screen?

To center the dropdownlist and the three textareas on the screen instead of sticking to the left, you can adjust the CSS by adding the following styles: Add "text-align: center;" to the parent container to center align the elements. Add "display: inline- ...

A div set to absolute positioning and a width of 100% will exceed the boundaries of a Bootstrap 4

I am attempting to position a div with 100 percent width within a Bootstrap 4.1 col using absolute positioning in order to attach it to the bottom. However, I am encountering an issue where it overflows on the right side of the screen. I have tried assigni ...

Increase the number of rows in the table after applying the angular limitTo function

I have been attempting to increase the number of rows in a table by adjusting the limitTo value: <button ng-click="showMoreQueues()">Show More </button> The corresponding function is as follows: $increaseRows = function(){ $rowLimit += 2 ...

Issues with the performance of the Responsive Image Slider

Having trouble with the carousel on my website. The Navigation Bar is working, but the image slider isn't showing up despite trying multiple approaches. Bootstrap is properly linked and I've read through the documentation for the Bootstrap Carous ...

Attach the div element firmly to the bottom of the webpage

I'm trying to place a second div under one with position absolute, but the two divs have different widths. The top div is centered and positioned absolutely, while the bottom div should span 100% of the width. Any suggestions on how to achieve this? ...

CSS selector for the 'second next' element, checkboxes and labels within MVC forms

I found a helpful resource that explains how to style checkboxes using CSS. It suggests using the + selector to target the label immediately following the checkbox: input[type="checkbox"]{} input[type="checkbox"] + label {} However, I encountered an issu ...

Encountered an Angular 1.6 Error: The requested URL at http://127.0.0.1:56035/[url] could not be found (404

I've hit a roadblock in my project and can't seem to figure out the issue. The file I'm working on is supposed to display an iframe video player with a YouTube embed. I tried using interpolation to construct the correct URL: <div class= ...

Is it possible to slide-toggle and alter the background color of a div when clicked?

Imagine having several div's on a webpage all with the ID of "Toggle". Each div contains two other smaller divs. "Headline" which is always visible. "Comment" which appears/disappears when the headline is clicked (initially hidden). How could I ac ...

What is the best way to incorporate two banners on the Magento homepage alongside my flexslider?

I recently made the switch from using a camera.js slider on my website to a Flexslider. The reason for this change was that I couldn't make the camera.js slider clickable, and I wanted to eliminate the 'Click here' button. However, now that ...

Using jQuery to vertically center a div

When a vertical menu on my website is clicked, it pops out from the left side of the screen. The content inside is vertically centered using CSS transformations. While expanding to show sub-items, everything remains centered. However, there's an issue ...

What is the method used by ng-controller to find the correct controller file?

I'm completely new to AngularJS. In my Visual Studio solution, there are multiple projects, and I want to transfer a view that uses a controller from one project to a new project within the same solution. However, I'm unsure if I need to move the ...

Can someone explain the method for displaying or concealing a menu based on scrolling direction?

https://i.stack.imgur.com/tpDx0.jpg I want to hide this menu when scrolling down and show it when scrolling up. The code for my menu bot is: <script> var previousScroll = 0; $(window).scroll(function(event){ v ...

Add a CSS class to a different element when hovering

I have the following HTML structure: <div class="container"> <div class="list-wrapper"> <ul> <li class="item-first"> </li> </ul> </div> <div class="description-wrapper"> <d ...

With a simple click of a button, I aim to have the ability to set a variable to true

I am looking to create a button that changes to true in the Script section. This will allow for a random number to be displayed in the paragraph element. <!doctype html> <html> <button id="button"> random number </button> ...

Spacing in CSS between the menu and its submenu elements

My current challenge involves creating space between the menu and the submenu. However, I've noticed that when there is this space, the submenu doesn't function correctly. It only works when the design has no gap between the menu and the submenu. ...

Struggling to combine select dropdown choices in one calculation

​​I'm currently working on a project where I need to create a website that multiplies three numbers selected from dropdown menus together. However, when I attempt to perform the calculation, I only get the result "1" displayed. I've spent sev ...

Paused session in web development

Currently, I am in the process of building a website using HTML and CSS and have encountered an issue. Within my CSS file, I have defined an ID called "full" which sets a wooden background after the sidebar and is intended to span across the entire page. A ...