ahr*_*erg 30 angular angular-animations
我正在尝试制作一个简单的动画,如下面的简单jQuery
animate({'left' : left_indent})
Run Code Online (Sandbox Code Playgroud)
我正在使用Angular2动画,但问题是如何将我的Component Class外的left_indent参数传递给动画触发器?
animations: [
trigger('flyInOut', [
state('for', style({
left: this.left_indent,
})),
transition('* => for', [
animate(2000, keyframes([
style({ opacity: 1, transform: 'translateX(0)', offset: 0 }),
style({ opacity: 1, transform: 'translateX(-300px)', offset: 0.5 }),
]))
]),
])
]
Run Code Online (Sandbox Code Playgroud)
Evg*_*nyV 51
现在有可能.
animations: [
trigger('flyInOut', [
state('for', style({
left: '{{left_indent}}', // use interpolation
}), {params: {left_indent: 0}}), // default parameters values required
transition('* => for', [
animate(2000, keyframes([
style({ opacity: 1, transform: 'translateX(0)', offset: 0 }),
style({ opacity: 1, transform: 'translateX(-300px)', offset: 0.5 }),
]))
]),
])
]
Run Code Online (Sandbox Code Playgroud)
更新(根据SplitterAlex答案):
在模板中(对于Angular <4.4.6):
<div [@flyInOut]="{value: triggerValue, left_indent: left_indent}"></div>
Run Code Online (Sandbox Code Playgroud)
对于Angular> = 4.4.6模板应该是
<div [@flyInOut]="{value: triggerValue, params: {left_indent: left_indent}}"></div>
Run Code Online (Sandbox Code Playgroud)
Spl*_*lex 12
对于Angular 4.4.6,接受的答案对我不起作用
您必须将param值包装在对象的模板中 params
更换:
<div [@flyInOut]="{value: triggerValue, left_indent: left_indent}"></div>
Run Code Online (Sandbox Code Playgroud)
附:
<div [@flyInOut]="{value: triggerValue, params: {left_indent: left_indent}}"></div>
Run Code Online (Sandbox Code Playgroud)
小智 7
我有一个解决方案。但是,仅当您尝试使用相同的动画多次使用已经知道的不同参数时,它才有用。
例如,我有可重复使用的动画来制作slideUp-slideDown效果。在折叠状态下,容器必须保持一定高度(我已经知道这些容器的高度)。
动画:
import { style, trigger, state, transition, animate } from "@angular/animations";
export const slideUpDownAnimation = (height) => {
return trigger(`slideUpDownAnimation${height}`, [
state('0', style({
overflow: 'hidden',
height: '*'
})),
state('1', style({
overflow: 'hidden',
height: `${height}px`
})),
transition('1 => 0', animate('400ms ease-in-out')),
transition('0 => 1', animate('400ms ease-in-out'))
]);
};
Run Code Online (Sandbox Code Playgroud)
在组件的类中:
import { slideUpDownAnimation } from "../../animations/slide-up-down.animation";
@Component({
moduleId: module.id,
selector: 'new-order',
templateUrl: './new-order.component.html',
animations: [
slideUpDownAnimation(32), // will return trigger named "slideUpDownAnimation32"
slideUpDownAnimation(60) // will return trigger named "slideUpDownAnimation60"
]
})
export class NewOrderComponent {...
Run Code Online (Sandbox Code Playgroud)
最后,在组件的html中:
<div class="header-fields"
[@slideUpDownAnimation32]="collapsedFields">
...
Run Code Online (Sandbox Code Playgroud)
<div class="line-group"
*ngFor="let group of lineGroups; let g = index"
[@slideUpDownAnimation60]="group.collapsed">
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不能用于动态参数,因为必须在decorator&html中定义它们。
归档时间: |
|
查看次数: |
11513 次 |
最近记录: |