ngIf and CSS transitions/animations in Angular 2
How to slide a div in from the right using CSS in Angular 2?
Notes
.transition{ -webkit-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out; -moz-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out; -ms-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out ; -o-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out; transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out; margin-left: 1500px; width: 200px; opacity: 0; } .transition{ opacity: 100; margin-left: 0; }
This code works fine if you just use [ngClass] to switch classes, and take advantage of opacity. But don't want that element to be rendered from the beginning, so I "hide" it with ngIf first, but then the transition won't work.
Update 4.1.0
uses the transition animation API, no longer need to use [hidden] or [*ngIf hidden].
Update 2.1.0
import { trigger, style, animate, transition } from '@angular/animations';
@Component({
selector: 'my-app',
animations: [
trigger(
'enterAnimation', [
transition(':enter', [
style({transform: 'translateX(100%)', opacity: 0}),
animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
]),
transition(':leave', [
style({transform: 'translateX(0)', opacity: 1}),
animate('500ms', style({transform: 'translateX(100%)', opacity: 0}))
])
]
)
],
template: `
xxx
`
})
export class App {
show:boolean = false;
}
Original answer
When the expression becomes false, *ngIf will remove the element from the DOM. An element that does not exist cannot be transitioned.
can be replaced by the hidden attribute:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3