A step-by-step guide on how to smoothly hide an element using ng-hide in AngularJS

Currently, I am able to toggle an element's visibility based on a boolean condition in my controller. However, I am looking for a way to smoothly fade out the element if the condition is true instead of instantly hiding it. Any suggestions on how to achieve this?

<div id='conversion-image' ng-hide="pages.length > 0">
    generating pages...
    <br />
    <img src='Images/ajax-loader-blue.gif' />
</div>

I have already included the ngAnimate dependency in my code.

var app = angular.module("app", ["ngAnimate"]);

Answer №1

If you want to achieve this effect using CSS and the ng-animate, you can follow these steps:

.fade-out.ng-hide {
  opacity: 0;
}

.fade-out.ng-hide-add, .fade-out.ng-hide-remove {
  transition: all linear 0.5s;
}

.check-element {
  border: 1px solid black;
  opacity: 1;
  padding: 10px;
}
<head>
  <meta charset="UTF-8">
  <link href="animations.css" rel="stylesheet" type="text/css">
  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
  <script src="//code.angularjs.org/snapshot/angular-animate.js"></script>      
</head>

<body ng-app="ngAnimate">
  Hide: <input type="checkbox" ng-model="checked" aria-label="Toggle ngHide"><br />
  <div class="check-element fade-out" ng-hide="checked">
    I fade out when your checkbox is checked.
  </div>
</body>

Check out the Angular ng-hide documentation for more information.

Answer №2

In the official AngularJS guidelines, it discusses:

How to Customize .ng-hide

The default behavior of the .ng-hide class is to apply display: none !important styling. If you want to modify the hiding functionality using ngShow/ngHide, you can easily override the styles for the .ng-hide CSS class. It's important to note that the appropriate selector to use is actually .ng-hide:not(.ng-hide-animate) in order to accommodate any additional animation classes that may be included.

Answer №3

Include the class class="animate-show animate-hide"

<div id='conversion-image' class="animate-show animate-hide" ng-
   hide="model.transaction.selectedDocument.pages.length > 0">
     generating pages...
    <br />
   <img src='Images/ajax-loader-blue.gif' />
</div>

Apply the styles listed below

<style>
    .animate-show,.animate-hide {
       -webkit-transition:all linear 1s;
       -moz-transition:all linear 1s;
       -ms-transition:all linear 1s;
       -o-transition:all linear 1s;
       transition:all linear 1s;
  }

   .animate-show.ng-hide-remove,
    .animate-hide.ng-hide-add.ng-hide-add-active {
        opacity: 0;
        display: block !important;
    }

    .animate-hide.ng-hide-add,
    .animate-show.ng-hide-remove.ng-hide-remove-active {
        opacity: 1;
        display: block !important;

</style>

Check out a plunker example at http://plnkr.co/edit/VoWwmHK57wtuyGl6npr0?p=preview

Answer №4

To create a fading effect on your div, simply apply the fade class and include a button to toggle the visibility of the content. You can customize the condition for hiding using ng-hide = "your_own_condition"

<div id='conversion-image' class="fade" ng-hide="condition">
    generating pages...
    <br />
    <img src='Images/ajax-loader-blue.gif' />
</div>

<button ng-click="condition=!condition">Toggle</button>

<style>
.fade.ng-hide {
  transition:0.5s linear all;
  opacity:0;
}
</style>

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

How can I send my response as JSON in the controller in Asp.net?

Whenever I use toastr for form notifications, I always seem to receive an error message even though the data is successfully saved in the database. I suspect that the error may be stemming from the controller because I am not sending back in JSON format. A ...

Error: Attempting to subscribe to a post request returned a null result

Every time I attempt to subscribe to a post request, the TypeError: result is null is returned My setup involves an Angular CLI connecting with a Spring Boot application for a simple login page. My goal is to save the response header in local storage. Be ...

The requested resource at http://localhost/Grafica/%7Bd.icon%7D/ was not found (Error 404)

Creating a tooltip in a weather chart, I want to display an image of the sky condition. Below is the HTML code: <div id="tooltip"> <table class="table table-condensed"> <tr><th>Time (local)</th><th data-text="d ...

Troubleshooting JavaScript for Sidebar Disappearance due to marginRight and display CSS Issue

I need to adjust the layout of my website so that the sidebar disappears when the window is resized below a certain width, and then reappears when the window is expanded. Here is the HTML code: <style type="text/css"> #sidebar{ width:290 ...

Having difficulty maintaining the consistent size of the MathJax math font in relation to the surrounding text

Issue I am currently using MathJax to display mathematical equations on my webpage: https://i.sstatic.net/EfvVq.png The problem I am facing is that I want the math font to appear larger than the surrounding text, as depicted in the image above. However, ...

Error: Invalid character encountered during login script JSON parsing

I found this script online and have been experimenting with it. However, I encountered the following error: SyntaxError: JSON.parse: unexpected character [Break On This Error] var res = JSON.parse(result); The problem lies in the file below as I am unf ...

Executing multiple axios.post calls in Vue to refresh the page

My code in App.Vue looks like this: <script> import axios from "axios"; import { baseApiUrl, userInfo } from "@/global"; export default { methods: { moveToLoginPage() { localStorage.removeItem(userInfo); this.$store.commit("setTok ...

The sequence of data and the final event triggered by http.createServer

const server = http.createServer(async (request, response) => { if (request.method === "POST") { var data = ""; request .on("data", async (chunk) => { console.log("1"); data + ...

Connecting an onclick event to trigger an external file

I have a scenario where I need to link an array of buttons with respective mp3 files. For example, if button number 5 is clicked, the 5th mp3 file should be played. How can I modify the code below to achieve this functionality? Any examples or suggestions ...

React: the function is returning as undefined

Description I am encountering an issue with a function in a functional component, as it keeps returning undefined. Despite having all the necessary data defined and accurate within the function including tableData and subtractedStats. This seems to be a ...

How to resolve: Preventing Ajax - Post request from executing repetitively

I am facing an issue with a table that is formatted using the Datatables script. There is a column in the table which contains icons for actions. When a user clicks on an icon, a modal is loaded and its content is fetched using the POST method. The modal ...

Tips for effectively running multiple XMLHttpRequests in a loop

While running XMLHttpRequest inside .map(), everything operates smoothly. list.map(function(file) { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () {} xhr.onerror = function () {} xhr.upload.addEventListener(& ...

Exposing a factory JS variable globally within an AngularJS environment

I am trying to access a variable created within a factory in another function in my AngularJS controllers. How can I achieve this and make the new calculated value available? The variable I want to use is result.data.bkor_payamount = result.data.bkor_paya ...

Troubleshooting the Issue of PHP Variables Not Being Assigned to Javascript Variables

I am currently struggling with an issue. I am trying to assign a PHP value to a variable in Javascript. Here is what I have attempted: <script> JSvariable = <?php echo $PHPvariable; ?>; </script> However, this approach is not yieldi ...

Creating a front-end button that triggers pdfcrowd's node.js API to generate PDFs is a simple and effective way to streamline the process

I recently integrated pdfcrowd into my MERN-stack app to allow users to download PDFs of the app pages. I followed the example provided in the pdfcrowd documentation and successfully generated a PDF file using Node APIs. var pdfcrowd = require("pdfcrowd") ...

Please input a number that falls within a specified range

I need help with two text inputs that are connected v-model to a ref object. I also have two other refs, minimum (100) and maximum(1000). My goal is to allow users to input values within the range of the minimum and maximum values provided. If the value en ...

removing / deactivating a filter in AngularJS

Is there a way to disable a filter once it's been applied? For example: <h3 class="mention" ng-bind-html="mention | getPlace" place-directive >&nbsp;<span>@</span>{{mention}}</h3> Filter: hungryApp.filter('getPlac ...

Using the await keyword in the useEffect() React Hook: A comprehensive guide on implementation

When using several async functions within useEffect(), I am struggling to properly include the await keyword to wait for their results // Assume func1 and func2 are defined elsewhere async function func1() {...}; async function func2() {...}; // Import f ...

Looking to eliminate the unchecked checkbox value from the list using jQuery?

After selecting a checkbox, the text/value is displayed in an li element. However, if I uncheck the first box and check the second one, I want to remove the text/value associated with the first checkbox as shown below: View Image Here Here is my code sni ...

Tips for designing a button that can execute a JavaScript function repeatedly

My goal is to rotate the numbers clockwise when the rotate button is clicked. I have included all the relevant code. I have created a button that triggers a javascript function when clicked. The problem is that the function only rotates the numbers once. ...