Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2/4 Animation on div height

I'll try to keep it short. For some reason, My animation seems to work fine on width/opacity style attribute yet it doesn't work on height attribute....

the animation -

trigger('Grow', [
transition(':enter', [   // :enter is alias to 'void => *'
  style({height:'0'}),
  animate(500, style({height:'*'})) 
]),
transition(':leave', [   // :leave is alias to '* => void'
  animate(500, style({height:0})) 
])
])

Changing the 'height' to width/opacity and the animation works great... any one knows wheres the issue? couldn't find relative answer anywhere .

an example of what I'm trying to achieve is like the animation in udemy, upon clicking on a section the div height expands the display all the videos - https://www.udemy.com/meteor-react/

thanks for reading .

Update - still doesn't work...

maybe its something with what I'm animating on?

<div class="animate" *ngIf="show" [@animate]> 
  <div  *ngFor="let video of section"><a>hi</a></div>
</div>

what happens is once I click, the div.animate is showing (by ngif) and fills with lots of lines that says 'hi'. I want that on div.animate show, to make the animation I specified.

help! ^^

like image 423
Ben Avatar asked Apr 19 '17 13:04

Ben


People also ask

How do I animate with CSS in angular?

In Angular, use the style () function to specify a set of CSS styles for use with animations. Collect a set of styles in an animation state, and give the state a name, such as open or closed. Let's create a new open-close component to animate with simple transitions.

How to animate the width and height of a div element?

Store the actual width and height of the div element on which animation is to be done using $ (selector).width () method. When the mouse pointer event is handle the .mouseenter () and .mouseleave () methods.

What is the @angular/animations module?

The functional API provided by the @angular/animations module provides a domain-specific language (DSL) for creating and controlling animations in Angular applications. See the API reference for a complete listing and syntax details of the core functions and related data structures. More on Angular animations link

How to define the duration of angular animation?

We can define the duration of angular animation in the following three ways. Using an integer value to describe the time in milliseconds. E.g., 1000. Using the string value to describe the time in milliseconds. E.g.: ‘1000ms’. Using the string value to describe the time in seconds. E.g.: ‘1s’.


Video Answer


2 Answers

You don't have any states defined in your trigger() function.

trigger creates a named animation trigger, containing a list of state() and transition() entries to be evaluated when the expression bound to the trigger changes (the expression being [@slideInOut]="helpMenu" in example below).

@Component({
  ...
  animations: [
    trigger('slideInOut', [
      state('in', style({
        overflow: 'hidden',
        height: '*',
        width: '300px'
      })),
      state('out', style({
        opacity: '0',
        overflow: 'hidden',
        height: '0px',
        width: '0px'
      })),
      transition('in => out', animate('400ms ease-in-out')),
      transition('out => in', animate('400ms ease-in-out'))
    ])
  ]
})
export class AComponent implements OnInit {

  helpMenu: string;

  constructor() { }

  ngOnInit() {
    this.helpMenu = 'out';
  }

  toggleHelpMenu(): void {
    this.helpMenu = this.helpMenu === 'out' ? 'in' : 'out';
  }

}

Then use it in your view like this:

<div [@slideInOut]="helpMenu">
   <h1>My Title</h1>
   <p>My paragraph</p>
</div>
<button (click)="toggleHelpMenu()">help</button>
like image 186
Yeysides Avatar answered Oct 18 '22 00:10

Yeysides


Angular animations cat work with *ngIf directive perfectly fine.

Right now there's a section in the official docs that describes how to do it.

The problem in your example is that template animation trigger is specified as [@animate], but in component's animations declaration actual trigger is Grow. Thus, template trigger needs to be specified as [@Grow].

app.component.ts

    trigger("grow", [ // Note the trigger name
      transition(":enter", [
        // :enter is alias to 'void => *'
        style({ height: "0", overflow: "hidden" }),
        animate(500, style({ height: "*" }))
      ]),
      transition(":leave", [
        // :leave is alias to '* => void'
        animate(500, style({ height: 0, overflow: "hidden" }))
      ])
    ])

app.component.html

<div class="test" *ngIf="show" [@grow]> <!-- Use exact same trigger name here -->
  <div *ngFor="let video of section"><a>hi</a></div>
</div>

Working demo: stackblitz

like image 20
im.pankratov Avatar answered Oct 18 '22 00:10

im.pankratov