我有一个动画,正在扩大悬停高度。我在ngOnInit中使用标签的高度,因为标签是自动高度的,并且不能通过以下命令正确设置动画:hover this is my ts @Component with the animation:
@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()">
发布于 2019-05-28 02:20:14
无法使动画工作,但您不能在动画中使用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'))
])
]
你必须为你定义的每个参数添加一个默认值(否则你会得到一个错误)。
我希望我能把你引向正确的方向(:
发布于 2019-05-28 02:02:51
我不明白如何将this
绑定到组件实例,因为它是在数组中使用的,而不是可以以某种方式绑定的函数。我在文档中也没有发现这种可能性。我认为你想要做的事情可以用CSS来完成。
@Component({
selector: 'app-create',
templateUrl: './create.component.html',
styleUrls: ['./create.component.css']
})
export class Create implements OnInit {
@ViewChild('button') button : ElementRef;
buttonHeight : number;
isActive = false;
animate() {
isActive = !isActive;
if (isActive) {
this.buttonState.nativeElment.height = `${this.buttonHeight}px`;
} else {
this.buttonState.nativeElment.height = '100%';
}
}
ngOnInit() {
this.buttonHeight = this.button.nativeElement.offsetHeight
}
.active {
transition: all 1.5s ease-out;
}
.inactive {
transition: all 1.5s ease-in;
}
<label [ngClass]="{active: isActive, inactive: !isActive}" (mouseover)="animate()" (mouseout)="animate()">
https://stackoverflow.com/questions/56330327
复制相似问题