kon*_*ban 5 css typescript angular angular8
正如问题所暗示的那样,我试图找出一种在页面加载后为元素设置动画的方法。我已经找遍了所有地方,但似乎有太多并且无法同时执行此操作,我希望得到一些指导。在移动设备中加载页面后,徽标应该会慢慢朝右上方移动并缩小尺寸(如果有意义的话)。
我正在寻找 Angular 等价物 $(document).ready(function() {}
根据建议,我已经使用过,ngAfterViewInit()但仍然无法正常工作。
以下 index-section.component.html
<section class="index-section">
<div [@logoMoveResize]="load_completed ? 'initial' : 'end'" class="index-logo-wrapper" [class.hideOnLoad]="isTrue">
<figure>
<img src="assets/icons/logo_mobile.svg" alt="urbanwheels logo">
</figure>
</div>
<div class="index-media-wrapper">
<div class="media-container">
<iframe src="https://player.vimeo.com/video/128014070?autoplay=1&color=ffffff&title=0&byline=0&portrait=0" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Itaque contra est, ac dicitis; Duo Reges: constructio interrete. Videsne quam sit magna dissensio?
</p>
</div>
</section>Run Code Online (Sandbox Code Playgroud)
而 index-section.component.ts
import { Component, OnInit, Inject, ViewChild } from '@angular/core';
import { trigger, state, animate, style, group, query, transition } from '@angular/animations';
@Component({
selector: 'app-index-section',
templateUrl: './index-section.component.html',
styleUrls: ['./index-section.component.scss'],
animations: [
trigger('logoMoveResize', [
state('initial', style({
transform: 'translateX(0%) translateY(0%) scale(1)',
})),
state('end', style({
transform: 'translateX(25%) translateY(-25%) scale(.3)',
})),
transition('initial <=> end', [animate('1s')]),
])
]
})
export class IndexSectionComponent implements OnInit {
load_completed = true;
innerWidth: any;
ngOnInit() {
this.innerWidth = window.innerWidth;
}
ngAfterViewInit() {
if ( this.innerWidth < 1000 ) {
this.load_completed = false;
}
}
}Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
在 component.ts 中设置一个变量
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.scss'],
animations: [
trigger('fadeInOut', [
state('void', style({
opacity: 0
})),
transition('void <=> *', animate(1000)),
]),
trigger('EnterLeave', [
state('flyIn', style({ transform: 'translateX(0)' })),
transition(':enter', [
style({ transform: 'translateX(-100%)' }),
animate('0.5s 300ms ease-in')
]),
transition(':leave', [
animate('0.3s ease-out', style({ transform: 'translateX(100%)' }))
])
])
]
})
export class SomeComponent implements OnInit {
load_completed = false;
ngOnInit(){
}
ngAfterViewInit(){
load_completed = true;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的 component.html
<div [@fadeInOut]="load_completed"> some random element you want to animate </div>
Run Code Online (Sandbox Code Playgroud)
如上例所示,您可以根据条件在需要时设置动画
这个答案为我提供了有关该问题所需的信息。正如 @Kevin LeStarge 所建议的,我的解决办法是:
setTimeout(()=> {
this.load_completed = true;
}, 0);Run Code Online (Sandbox Code Playgroud)
或者正如 @Lijo 建议使用AfterContentChecked生命周期钩子:
import { ChangeDetectorRef, AfterContentChecked} from '@angular/core';
constructor(
private cdref: ChangeDetectorRef) { }
ngAfterContentChecked() {
this.cdref.detectChanges();
this.load_completed = true;
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4360 次 |
| 最近记录: |