我有两个div(父和子),我想根据自定义状态(close => open&& open => close)做一个特殊的动画:
好消息是当状态变为=>时,动画按预期工作(对于两个div) close =>开。
坏消息是,当状态从open =>开始时,动画不起作用(仅适用于子div)关闭。
说够了,这就是我所做的:
视图HTML :
<div [@openClose]="opened ? 'open' : 'close'" class="parent">
<div [@animateChild]="opened ? 'open' : 'close'" class="child">
<p>Child content</p>
</div>
</div>
在component.ts :
@Component({
selector: 'app-my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css'],
animations: [
trigger('openClose', [
state('open', style({
opacity: 1,
visibility: 'visible',
})),
// when we go from close to open do these steps
transition('close => open', [
query(':self', [// animate div itself
animate('200ms ease-in', style({
opacity: 1,
visibility: 'visible',
}))
]),
query('@animateChild', animateChild())// then animate children (.child)
]),
transition('open => close', [
query(':self', animate('200ms ease-in')),
query('@animateChild', animateChild()),
]),
]),
trigger('animateChild', [
state('open', style({ opacity: 1, transform: 'scale(1)' })),
transition('close => open', [
animate('100ms ease-out')
]),
transition('open => close', [
style({ transform: 'scale(0.3)', opacity: 0 }),
animate('100ms ease-out')
])
])
]
})
export class MyComponent implements OnInit {
opened: boolean;
constructor() { }
ngOnInit() {}
open(){
this.opened = true;
}
close(){
this.opened = false;
}
}
在CSS文件中:
.parent{
width: 100%;
height: 100vh;
background: black;
opacity: 0;/* initialize opacity to 0 for the parent */
}
.child{
width: 50%;
background: white;
transform: scale(0.3);/* initialize scale to 0.3 for the child */
opacity: 0; /* and opacity to 0 to be invisible when component initialized */
}
这是Stackblitz的例子:
https://stackblitz.com/edit/angular-bjuzyr
我在这里做错了什么?
答案 0 :(得分:1)
请您尝试下面的动画代码:
animations: [
trigger('openClose', [
state('open', style({
opacity: 1,
visibility: 'visible',
})),
state('close', style({
opacity: 0,
visibility: 'visible',
})),
// when we go from close to open do these steps
transition('* => *', [
animate('200ms ease-in'),
]),
]),
trigger('animateChild', [
state('open', style({ opacity: 1, transform: 'scale(1)' })),
state('close', style({ opacity: 0, transform: 'scale(0.3)' })),
transition('* => *', [
animate('100ms ease-out')
])
])
]
答案 1 :(得分:0)
好吧,我想出了解决方案:
我只需要在close
触发器中设置animateChild
状态的样式:
trigger('animateChild', [
state('open', style({ opacity: 1, transform: 'scale(1)' })),
state('close', style({ opacity: 0, transform: 'scale(0.3)' })),// I must add this lane
transition('close => open', animate('100ms ease-out')),
transition('open => close', animate('100ms ease-out'))
])