英文:
Creating an Angular Animation that can use an @Input() value?
问题
能否通过一个函数传递自定义参数来创建一个带有Angular动画的组件,然后在组件中使用生成的动画?
例如在这个演示中,动画是在单独的 animations.ts
中创建的:
import {
trigger,
state,
style,
transition,
animate,
group,
} from '@angular/animations';
export const SlideInOutAnimation = [
trigger('slideInOut', [
state(
'in',
style({
width: '50%',
opacity: '1',
visibility: 'visible',
})
),
state(
'out',
style({
width: '50px',
opacity: 0.3,
visibility: 'visible',
})
),
transition('in => out', [
group([
animate(
'600ms ease-in-out',
style({
width: '50px',
})
),
]),
]),
transition('out => in', [
group([
animate(
'1ms ease-in-out',
style({
visibility: 'visible',
})
),
animate(
'600ms ease-in-out',
style({
width: '50%',
})
),
animate(
'800ms ease-in-out',
style({
opacity: '1',
})
),
]),
]),
]),
];
然后可以在声明了动画的组件中通过 @Component
装饰器的 animations
属性导入并使用此动画。
但是如果我们想要获取一个 @Input
属性的值并将其传递给动画,应该如何做?
例如,如果我们有类似以下的内容:
@Input()
width: '50%';
我们将如何将此参数传递给动画?
英文:
Is it possible to create an Angular Animation with custom parameters passed in via a function, and then using the resulting animation within a component?
For example in this demo the animation is created in a separate animations.ts
like this:
import {
trigger,
state,
style,
transition,
animate,
group,
} from '@angular/animations';
export const SlideInOutAnimation = [
trigger('slideInOut', [
state(
'in',
style({
width: '50%',
opacity: '1',
visibility: 'visible',
})
),
state(
'out',
style({
width: '50px',
opacity: 0.3,
visibility: 'visible',
})
),
transition('in => out', [
group([
animate(
'600ms ease-in-out',
style({
width: '50px',
})
),
]),
]),
transition('out => in', [
group([
animate(
'1ms ease-in-out',
style({
visibility: 'visible',
})
),
animate(
'600ms ease-in-out',
style({
width: '50%',
})
),
animate(
'800ms ease-in-out',
style({
opacity: '1',
})
),
]),
]),
]),
];
And this animation can then be imported an used in a component that declares the animation in the animations
property of the @Component
decorator.
But what if we want to take an @Input
property value and pass that to the animation.
For example if we have something like:
@Input()
width: 50%;
How would we pass this parameter to the animation?
答案1
得分: 1
"parameters" 不那么难的动画
想象一些非常简单的
animations: [
trigger('custom', [
transition('*=>*', [
style({ width: `{{width}}` }),
animate('500ms ease-in', style({ width: '100%' })),
], { params: { width: '0' } }),
])
]
请注意我们在 "params" 中包含了 "parameters"(它是一个对象),当我们想要使用它时,它被 {{
}}
包围。默认情况下需要指示参数。
唯一的,当在 .html 中定义动画时需要传递参数,例如,如果你有两个变量 "value" 和 "width",你可以在 HTML 中这样写:
<div class="header" [@custom]="{value: value, params: {width: width}}" >
然后,当我们更改 "value" 时,动画开始
<button (click)="width='50%'; value=!value">width: 50%</button>
<button (click)="width='0'; value=!value">width: 0</button>
查看 stackblitz
在 stackblitz 中,我在组件中使用了一个变量,但你也可以使用一个输入变量,但请记住,如果你希望动画开始,你需要更改 "value"(除非你使用 :enter 和 :leave)。
英文:
An animation with "parameters" are not so difficult
Imagine some very simple
animations:[
trigger('custom',[
transition('*=>*', [
style({ width: `{{width}}` }),
animate('500ms ease-in', style({ width: `100%` })),
],{params:{ width: "0"}}),
])
]
See that we include the "parameters" in "params" (that it's an object) and when we want to use it's enclosed by {{
}}
. It's necesary indicate a params by defect
The only, when define the animation in the .html is pass the parameters, e.g. if you have two variables "value" and "width" you can have a html like
<div class="header" [@custom]="{value:value,params:{width:width}}" >
Then, when we change the "value" the animation begins
<button (click)="width='50%';value=!value">width:50%</button>
<button (click)="width='0';value=!value">width:0</button>
See stackblitz
In the stackblitz I use a variable in the component, but you can use an Input, but rememeber that if you want animation begings you need change the "value" (unless you use :enter and :leave)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论