What is the bare minimum and Angular4's native way to slide in and slide out a container element?
e.g.
<div ngIf="show">
<!-- Content -->
</div>
Slide In Content (from top to down just like jQuery.slideDown()) when show turns to true.
Slide Out Content (suitably with ease-out effect) when show turns to false.
I answered a very similar question, and here is a way of doing this :
First, create a file where you would define your animations and export them. Just to make it more clear in your app.component.ts
In the following example, I used a max-height of the div that goes from 0px (when it's hidden), to 500px, but you would change that according to what you need.
This animation uses states (in and out), that will be toggle when we click on the button, which will run the animtion.
animations.ts
Then in your app.component, we import the animation and create the method that will toggle the animation state.
app.component.ts
And here is how your app.component.html would look like :
slideInOut refers to the animation trigger defined in animations.ts
Here is a StackBlitz example I have created : https://angular-muvaqu.stackblitz.io/
Side note : If an error ever occurs and asks you to add BrowserAnimationsModule, just import it in your app.module.ts:
First some code, then the explanaition. The official docs describing this are here.
In your template:
I found the angular way a bit tricky to grasp, but once you understand it, it quite easy and powerful.
The animations part in human language:
->then animate the translateY value until we are at 0%, where the element would naturally be.
When the element is removed, animate the translateY value (currently 0), to -100% (off screen).
The easing function we're using is ease-in, in 200 milliseconds, you can change that to your liking.
Hope this helps!
Actually the minimum amount of Angular to be used (as requested in the original question) is just adding a class to the DOM element when
show
variable is true, and perform the animation/transition via CSS.So your minimum Angular code is this:
With this solution, you need to create CSS rules for the transition, something like this:
If you have retro-browser-compatibility issues, just remember to add the vendor prefixes in the
transition
s.See the example here