作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个动画,可以在悬停时扩展高度。我在 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/
我有一个动画,可以在悬停时扩展高度。我在 ngOnInit 中获取标签的高度,因为该标签是自动高度,并且仅通过 :hover 这是我的带有动画的 ts @Component 无法正确设置动画: @Co
我是一名优秀的程序员,十分优秀!