Incorporating sliding animation into Angular's ng-repeat directive

I am currently working on my code to create multiple divs with a hide button in each one. When the hide button is clicked, I want the corresponding div to fade away and the divs below it to smoothly move up to fill the space left behind.

Below is the snippet of my HTML code:

<body ng-app="task" ng-controller="repeat">
 <div ng-repeat='x in array' ng-hide="x.show">
  <p>{{ x.text }}
  </p>
  <button ng-click="toggle($index)">Hide</button>
 </div>
</body>

The JavaScript file contains the following script:

var app = angular.module('task');
app.controller('repeat',function($scope){
 $scope.array = [{
     show: true,
     text:'Sample Text 1'},
    { 
     show: true,
     text:'Sample Text 2'},
    { 
     show: true,
     text:'Sample Text 3'}];

 $scope.toggle = function(){
  $scope.array[index].show = false ;
 };
})

Lastly, this is the CSS code that I have included:

.ng-hide-add      { animation:1s fade ease; }

@keyframes fade{
  0% {
      opacity: 1;
      }

  100% {
        opacity: 0;
       }
 }

I have researched various slider animations online and tried to modify them for my project without success so far.

If you have any suggestions on what additional code I should implement to achieve the desired animation effect, please share your insights. Your input would be greatly valued. Thank you!

Answer №1

Here is a snippet of CSS code you can try:

.ng-leave {
  -webkit-animation: fadeOutLeft 1s;
  animation: fadeOutLeft 1s;
}
.ng-enter {
  -webkit-animation: fadeIn 0.5s;
  animation: fadeIn 0.5s;
}

Answer №2

Feel free to give this a try.

Check out the Plunker link

JS :

directive("divAnimate",function(){

  return {

    link : function(scope,ele,attr){

      ele.bind('click',function(){

         $(this).parent().fadeOut()
      })

    }
  }
})

HTML :

<body ng-app="task" ng-controller="repeat">{{}}
 <div  ng-repeat="x in array" ng-show="x.show">
  <p>{{ x.text }}
  </p>
  <button  div-animate>Hide</button>
 </div>
</body>

Instead of using fadeOut(), feel free to experiment with different jQuery methods for animation effects.

Answer №3

Consider incorporating transitions in your CSS styling. http://jsfiddle.net/bzr1fc5v/10/

.fade { 
    transition-property: opacity, height;
    transition-duration: 0.5s, 0.5s;
    transition-delay: 0s, 0.5s;
    opacity:0;
    height: 0;
}
div { height: 50px; }

This is how the HTML should look like:

<div ng-app="task" ng-controller="repeat">
 <div ng-repeat='x in array track by $index' ng-class="[x.show ? '' : 'fade']" >
  <p>{{ x.text }}</p>
  <button ng-click="toggle($index)">Hide</button>
 </div>
<div>

Note that setting the height of the div may be necessary. It's also recommended to add vendor prefixes for older browser support.

-webkit-
   -moz-
    -ms-
     -o-

To ensure the element hides after the transition, consider adding an event handler.

$('div').on('transitionend', function(e){ if(e.originalEvent.propertyName == 'height') $(e.target).hide(); })

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

AngularJS interprets expressions in the 'action' attribute

This afternoon I encountered a rather peculiar behavior with AngularJS. If "//" is present in an expression within the "action" attribute of a form, Angular will throw an interpolate error. Take a look at the code snippet below. When you run this code, t ...

What is the best way to showcase entire content over a background image on a mobile device?

Hello, I am facing an issue with displaying content on an image in desktop view that does not resize properly when the browser window is resized. The problem lies in setting a background image with full content. Can you please take a look at my images belo ...

Utilize AngularJS to transform images into base64 format

My project involves allowing users to upload their images and then converting them into a specific format before sending them to a .Net RESTful service. As I am new to AngularJS, I could use some assistance with this task. Any help would be greatly appre ...

Troubleshooting: JavaScript code not functioning properly with variable input instead of fixed value

I have encountered an issue with a JS function that I'm using. The function is shown below: // A simple array where we keep track of things that are filed. filed = []; function fileIt(thing) { // Dynamically call the file method of whatever ' ...

What are the steps to integrate Laravel and AngularJS?

Exploring the integration of Laravel with AngularJS has lead me to ponder on the most effective way to structure such a project. Should I (A) opt for a single domain where an API is consumed directly from the Laravel project, or (B) have website.com and a ...

Using Testcafe to extract the value attribute of a hidden input element

Is there a way to retrieve the value of an <input>, especially what is contained within its value attribute even if the input is not visible? I am using testcafé, but it seems like this could be a challenge. Does anyone have any suggestions or opti ...

How can I identify duplicate values within two distinct javascript objects?

Consider two sets of JavaScript arrays containing objects: var allUsers=[{name:"John",age:25},{name:"Emily", age:30},{name:"Michael",age:22}] and var activeUsers=[{name:"John",status:"active"},{name:"Sarah", status:"active"}] I am attempting to iterat ...

What is the recommended approach for structuring multiple conditional renderings within a single "return" statement in React?

I am currently working with React and React Three Fiber, and my goal is to create a side bar containing 12 options. These options will be used to show or hide 12 different meshes on the canvas. The logic I am aiming for is that when a user selects an optio ...

Can CKEditor be integrated with Mutation Observer? If so, how can this be achieved?

Trying to detect changes in the current CKEditor content. The goal is to identify which element's content has been modified when a user writes multiple paragraphs. Not well-versed in JavaScript or jQuery, but managed to come up with this code after s ...

Run JavaScript code when the HTML file has been modified for the second time

After browsing through this online forum, I stumbled upon a helpful solution. See the code snippet below for reference. However, I encountered a problem in my case. The script in question is essentially monitoring itself. The code resides in index.html an ...

The body tag mysteriously disappears after the function is called within the $().html() command

As I delve into scraping the source code of a website, I encounter an interesting phenomenon. Initially, when I print out the complete source code, everything appears as expected. However, upon attempting to print an actual DOM, I notice a slight change i ...

Changing the URL structure by hiding the folder hierarchy in the address bar using AngularJS and PHP

I am dealing with a unique situation on my AngularJS-PHP website. I have two different index pages - "index.php" and "index.html". In my login.tpm.html file, I have a href link that points to "/member/somePage", which triggers an action in index.php. Fro ...

Having trouble with the width:auto attribute in IE7?

CSS #wrapper .tn{ width:auto; height:20px; line-height:20px; float:left; padding:5px 2px 5px 5px; margin:0 5px 10px; font-weight:bold; background:#f0f0f0; } HTML <div id="wrapper"> ...

Breaking apart images with HTML5 canvas

I am currently working on creating a sliding puzzle piece game. By utilizing the canvas element, I successfully divided the image into multiple pieces. To shuffle these pieces, I stored their coordinates in an array, randomized the coordinates, and then up ...

Arrange the select default option using AngularJS as the first option

I've been working on a project using Angular where I fetch data from a JSON file and push it to an array object. This data is then displayed in the options of a select element. My issue is: The default option with selection appears first, but I only ...

Issues with sending emails through Nodemailer in a Next.js project using Typescript

I'm currently working on a personal project using Nodemailer along with Next.js and Typescript. This is my first time incorporating Nodemailer into my project, and I've encountered some issues while trying to make it work. I've been followin ...

Uploading files in ASP.NET MVC without creating a view, utilizing Valums Ajax Uploader technology

I recently completed a tutorial on ASP.NET MVC file uploads using Valums plugin and made sure to place all the necessary js, css, and gif files in their respective folders. However, I am facing an issue where the view is not displaying anything. <link ...

Tips for enabling menu wrapping with up and down arrow keys

In the Bootstrap 5 menu, when you press the up arrow key on the first item or the down arrow key on the last item, nothing happens. Is there a way to configure the up arrow to move to the last item in the menu if pressed on the first item, and for the dow ...

What is the reason for this assignment not being activated?

After going through the official xstate tutorial, I decided to create my own state machine inspired by a post on dev.to by a member of the xstate team. Everything is working fine except for the fact that the output is not being updated. It seems like the ...

Tips for enhancing shadow clarity in three.js

Within a scene, there are multiple objects that I want to move across a wide plane that is capable of receiving shadows. However, when using only one point light or a very large directional light, the shadows cast by the objects tend to have rough edges, e ...