gpt4 book ai didi

javascript - 在 @compnent 动画 Angular 7 中使用类变量

转载 作者:行者123 更新时间:2023-11-28 17:04:12 25 4
gpt4 key购买 nike

我有一个动画,可以在悬停时扩展高度。我在 ngOnInit 中获取标签的高度,因为该标签是自动高度,并且仅通过 :hover 这是我的带有动画的 ts @Component 无法正确设置动画:

@Component({
selector: 'app-create',
templateUrl: './create.component.html',
styleUrls: ['./create.component.css'],
animations: [
trigger('buttonState', [
state('inactive', style({
height: '100%'
})),
state('active', style({
height: this.buttonHeight
})),
transition('inactive => active', animate('1.5s ease-in')),
transition('active => inactive', animate('1.5s ease-out'))
])
]
})

问题是 this.buttonHeight 给我无法读取未定义的属性“buttonHeight”。我猜想“this”不是 @Compnent 中的类或者尚未定义。我怎样才能让它发挥作用?

这就是我测量高度的方法:

  @ViewChild('button') button : ElementRef;
buttonHeight : number;

ngOnInit() {
this.buttonHeight = this.button.nativeElement.offsetHeight
}

在 html 中:

<label [@buttonState]="state" (mouseover)="toggleState()" (mouseout)="toggleState()">

最佳答案

无法使动画正常工作,但您不能在动画中使用 this.buttonHeight。您必须添加一个参数:

HTML

<label [@buttonState]="{value: state, params: {buttonHeight: buttonHeight}}" (mouseover)="toggleState()"
(mouseout)="toggleState()">LABEL</label>

TS

animations: [
trigger('buttonState', [
state('inactive', style({
height: '100%'
})),
state('active', style({
height: "{{buttonHeight}}px"
}), { params: { buttonHeight: "4" } }),
transition('inactive => active', animate('1.5s ease-in')),
transition('active => inactive', animate('1.5s ease-out'))
])
]

您必须为您定义的每个参数添加默认值(否则会出现错误)。

我希望我能引导你走向正确的方向(:

关于javascript - 在 @compnent 动画 Angular 7 中使用类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56330327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com