gpt4 book ai didi

css - Angular 8中页面加载后触发动画,ExpressionChangedAfterItHasBeenCheckedError

转载 作者:行者123 更新时间:2023-12-01 19:50:08 27 4
gpt4 key购买 nike

正如问题所暗示的,我正在尝试找出一种在页面加载后使元素动画化的方法。我已经查遍了所有地方,但似乎太多而且无法同时执行此操作,我希望得到一些指导。页面在移动设备中加载后, Logo 应缓慢向右上角移动,并且尺寸也会缩小(如果有意义的话)。

我正在寻找 $(document).ready(function() {}

的 Angular 等效项

根据建议,我已经使用了 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>

还有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;
}
}
}

这是我收到的错误:

enter image description here

最佳答案

在 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;
}

}

在你的component.html中

<div [@fadeInOut]="load_completed"> some random element you want to animate  </div>

如上面的示例,您可以根据条件在需要时制作动画

关于css - Angular 8中页面加载后触发动画,ExpressionChangedAfterItHasBeenCheckedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58994336/

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