Using angular's ngShow along with animate.css for dynamic animations

I am having trouble animating the display and concealment of an element using both animate.css and angular.

Despite reading through this question on Stack Overflow and going over the angular documentation for ngShow and ngAnimate, I am still unable to achieve the desired result.

I attempted a setup on plunker, however, it did not work as expected.

app.js

var app = angular.module('plunker', ['ngAnimate']);
app.controller('MainCtrl', function($scope) {
  $scope.show = true;
});

index.html

<body ng-controller="MainCtrl">
    <p>Show: {{show}}</p>
    <div class="show" ng-click="show = !show" ng-show="show === true">Show is true</div>
    <div class="hide" ng-click="show = !show" ng-show="show === false">Show is false</div>
</body>

style.css

.show.ng-hide-add {
   animation: fadeOut 5s linear;
}

Upon clicking "Show is true" (to hide it), there is a delay of 5 seconds before hiding. While something is happening, it does not fade out as intended.

I managed to make it work by adding the following CSS:

.show.ng-hide-add {
    opacity: 1.0;
    display: block !important;
    transition: opacity 1s;
}

.show.ng-hide-add-active {
  opacity: 0;
}

However, I prefer not to use this approach. Instead, I want to utilize animate.css's keyframes like fadeIn, fadeOut, etc.

Here is the plunker link demonstrating the issue.

What am I missing? How can I incorporate animate.css's keyframe animations with angular's ngAnimate?

Answer №1

To achieve the desired effect, you should utilize the .ng-hide class. This is the class that is applied when the condition in ng-show evaluates to false or in ng-hide to true.

Here is an example of how you can modify your code:

.show.ng-hide,
.hide.ng-hide{
  opacity: 0;
  transition: all linear 0.5s;
}

.show,
.hide{
  transition: all linear 0.5s;
  opacity:1;
}
<p>Show: {{show}}</p>

<div class="show" ng-click="show = !show" ng-show="show">Show</div>
<div class="hide" ng-click="show = !show" ng-hide="show">Hide</div>

-

EDIT:

If you wish to incorporate animate.css classes, such as .fadeIn and .fadeOut, you will need to define the corresponding keyframes in your CSS.

Below is the CSS code needed for this implementation:

.show.ng-hide,
.hide.ng-hide{
  animation-name:fadeOut;
  animation-duration: .5s;
}

.show{
  animation-name:fadeIn;
  animation-duration: .5s;
}

IMPORTANT NOTE: To ensure proper functionality in the plunker, I manually linked version 3.5.1 instead of using the suggested 3.2.0 version from the external library finder. This was achieved by adding the following code to the HTML file:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.css" />

-

You can find a working Plunker with the full code here

Answer №2

Modify your code like this

<div ng-show="display">
    <div class="display-div" ng-click="display = !display">Display</div>
</div>
<div ng-show="!display">
    <div class="hide-div" ng-click="display = !display" >Hide</div>
</div>

.display-ng-hide{
    opacity: 0;
    transition: all linear 0.5s;
}
.display-div{
  transition: all linear 0.5s;
  opacity:1;
}

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

Troubleshooting hidden element display issues in IE7 with JQuery

My show/hide function is not working in IE7, however it works fine in other browsers. Can someone please assist me? Am I doing something incorrectly? <div id="secondary_nav" class="box"> <ul> <li>< ...

AngularJS translation: creating dynamic strings

I am utilizing the angular-translate module for translation purposes and I am trying to create a dynamic string as shown below. Translation key: "MY_TRANS_KEY": "Answer must include all of the following: {{limit}}" Html: <span class="warningSt ...

The reason the CSS direct descendant selector does not affect Angular components

We are working with a basic main.html. <app> <sidebar></sidebar> <main-content> <router-outlet></router-outlet> </main-content> </app> After loading a component through routing (changing the ...

I attempted to initiate gulp serve and received an error stating 'No gulpfile found'. As a novice in Nodejs, I am uncertain whether the gulp path was inputted incorrectly

Perhaps the issue lies with the path of gulp. However, I cannot say for certain. import gulp from 'gulp'; import browserSync from 'browser-sync'; import plumber from 'gulp-plumber'; import sass from 'gulp-sass'; imp ...

leveraging npm packages in Vue single page applications

I recently developed a Vue.js application using vue-loader and now I am trying to integrate an npm package that I have installed. Here is the code snippet: var x = require('package-name') Vue.use(x) However, I encountered the following ...

The top content above the Bootstrap navbar fails to remain fixed in place

Seeking assistance with a Bootstrap 4 navbar and header setup. The goal is to have the navbar stick to the top while making the header disappear on scroll. In my functions.php file, I've enqueued Jquery for this project. Below is the CSS found in st ...

Tips for submitting an e-mail through an HTML form with the help of Node.js and Gulp

Today, I've been tackling the challenge of creating an HTML contact form that can send an email using Node.js and Gulp. However, I'm struggling to find a solution. Using a relative path to a simple .PHP file doesn't seem to be working, so it ...

Tips for transferring JSON values into an array

What is the best way to convert JSON values into an array? I am using an AJAX request to fetch the JSON data. $.getJSON( "data/data.json", function( data ) { var obj=data["DataContainer"]["profit"]; for(i in obj){ var ...

What is the purpose of the video-js endcard plugin incorporating native controls into the player?

Endcard is a unique plugin designed for the video-js html5 video player that adds clickable links to the end of a video. (More information on endcards can be found here: https://github.com/theonion/videojs-endcard). To implement an endcard, simply include ...

What is the limitation in transmitting data as a JavaScript object rather than a JSON object?

As a newcomer to expressjs, I recently developed an application using it. Interestingly, when I send data in JSON format through POSTMAN, the application successfully returns the data. However, when I try to send data as a JavaScript object in the request ...

What are some strategies for maintaining the order of my elements on a mobile webpage?

Check out my awesome website: maitxhomes.com/main.html The layout looks amazing on desktop browsers, but it's a mess when viewed on my phone. The content in the blue section and footer just stacks in a long column that goes all the way down the scre ...

Navigating the intricacies of passing a complex C# object through FromQuery

I have a client-side code snippet showcasing an object setup as follows: { "selectedItems": [ 4016, 3937 ], "selectedStatuses": [], "search": "foo" } My intention is to match this structure with a C# DTO: public class FilterModel { p ...

In JavaScript, you can add or replace an object in an array of objects by checking for a specific property

I am looking for a more concise way to add or replace an object within an array. var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] Here is a new object: var mynewObject = {uid: 1, name: " ...

The uncomplicated bar graph and its corresponding axis line up perfectly beside each other in an SVG diagram

I'm currently working on a stacked bar chart in my CodePen project found here: https://codepen.io/a166617/pen/qBXvzQd However, I am facing an issue where the graph is not aligned properly with the x-axis and y-axis lines. The barchart seems to be sli ...

Getting started with arrays: A beginner's guide

Greetings and thank you for taking the time to read my post today! I am relatively new to the world of programming, but I have been diving into various online courses and exploring javascript and nodejs in recent weeks. The array below is a crucial compon ...

Submitting a Form with JQuery/AJAX and PHP

As I navigate through the world of form submissions in JQuery/AJAX and PHP, I find myself facing some challenges. While following online tutorials to learn, there are a few roadblocks that have cropped up. My goal is to create a form that can process subm ...

Determine whether an object exists within another object and merge its value into the formatted object

When filling out a form, I have a formattedData object that holds a deep copy of the original data object. If a field in the form is changed, I must update the formatted data object with properties from the values object. To simulate this scenario, I crea ...

Is it possible to use JavaScript to toggle the visibility of the date picker when using the HTML5 input type=date?

Looking to personalize the HTML5 input type="date" element by integrating a separate button that triggers the visibility of the date picker dropdown. Struggling to find any information on this customization. Any assistance would be greatly valued! ...

Tips on preventing scroll events from propagating in JavaScript?

Consider the following example: HTML: <div id="A"> <div id="B"> </div> </div> If a user hovers over element B and starts scrolling, we want to prevent the scroll event from affecting element A. How do we stop the scroll e ...

Instructions on creating a 5-second delay with a function in JavaScript

Is there a way to create a function that appears after 5 or 6 seconds? function displayAfterDelay() { setTimeout(function() { alert("This function will display after 5 seconds"); }, 5000); } displayAfterDelay(); ...