简体   繁体   English

几秒钟后隐藏div元素Angular 7

[英]Hide div element after a few seconds Angular 7

So I am having trouble understanding how to do this and how to approach this issue... I have a scroll animation on my page and I want to hide that div with animation after 3 seconds when it comes into view.所以我很难理解如何做到这一点以及如何解决这个问题......我的页面上有一个滚动动画,我想在 3 秒后用动画隐藏那个 div。

My HTML code for animated element is:我的动画元素的 HTML 代码是:

<div class="scroll-animation-wrapper">
  <div class="scroll-animation">
   <span></span>
   <span></span>
   <span></span>
  </div>
</div>

Code in my Typescript is:我的打字稿中的代码是:

hideAnimatedDiv() {
        const animatedDiv = document.getElementsByClassName('scroll-animation');
        this.animatedDiv = true;
        setTimeout(() => {
            console.log('hide');
            this.animatedDiv = false;
        }, 3000);
    }

I keep getting this error:我不断收到此错误:

ERROR in src/app/configurator/configurator.component.ts(577,14): error TS2339: Property 'animatedDiv' does not exist on type 'ConfiguratorComponent'.
src/app/configurator/configurator.component.ts(580,18): error TS2339: Property 'animatedDiv' does not exist on type 'ConfiguratorComponent'.

Your code is trying to find animatedDiv property as global, because you have used this.animatedDiv instead try like this.您的代码正在尝试将animatedDiv属性查找为全局属性,因为您已使用this.animatedDiv代替尝试这样。

HTML HTML

<div class="scroll-animation-wrapper">
  <div id="scroll-animation" class="scroll-animation">
    <span>a</span>
    <span>b</span>
    <span>c</span>
  </div>
</div>

TS TS

ngOnInit() {
   this.hideAnimatedDiv()
}

hideAnimatedDiv() {
   let animatedDiv = document.getElementById('scroll-animation');
   animatedDiv.style.display = 'block';
   setTimeout(() => {
     console.log('hide');
     animatedDiv.style.display = 'none';
   }, 3000);
}

To hide any element, we need to use style.display property of that element, like shown in the code.要隐藏任何元素,我们需要使用该元素的style.display属性,如代码所示。

Note : I have used ngOnInit function to make sure that div is hidden after component load only.注意:我使用ngOnInit函数确保仅在组件加载后隐藏div

If you want to hide div I would suggest you this:如果你想隐藏 div 我建议你这样做:

<div class="scroll-animation-wrapper">
  <div class="scroll-animation" *ngIf="isDisplayed">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

As you can see I added " *ngIf="isDisplayed" ".如您所见,我添加了“ *ngIf="isDisplayed" ”。 You should add the property "isDisplayed" to the component.您应该将属性“isDisplayed”添加到组件中。

public isDisplayed: boolean = true;

hideAnimatedDiv() {
    setTimeout(() => {
        this.isDisplayed = false;
    }, 3000);
}

If you want to add animation in Angular, the best way is to implement it as Angular Team suggests :如果你想在 Angular 中添加动画,最好的方法是按照Angular 团队的建议来实现它:

With this specific case I would use ":enter" ":leave" approach: https://angular.io/guide/transition-and-triggers#use-of-ngif-and-ngfor-with-enter-and-leave对于这种特定情况,我将使用 ":enter" ":leave" 方法: https ://angular.io/guide/transition-and-triggers#use-of-ngif-and-ngfor-with-enter-and-leave

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM