【问题标题】:Implement animation function from one component in different other components in Angular在Angular中的不同其他组件中从一个组件实现动画功能
【发布时间】:2018-08-05 07:45:37
【问题描述】:

我正在尝试将我在app.component.ts 中编写的动画功能实现到不同的其他组件中。实际上,我只想拥有这一个功能并在其他几个组件中实现它,而不是一遍又一遍地编写该功能。我不知道这是否是执行此操作的正确方法,或者是否有更好的方法?

app.component.ts:

import { Component, OnInit, HostListener, ElementRef } from "@angular/core";
import { trigger, state, style, animate, transition } from 
"@angular/animations";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
  animations: [
    trigger("scrollAnimationMain", [
      state(
        "show",
        style({
          opacity: 1,
          transform: "translateX(0)"
        })
      ),
      state(
        "hide",
        style({
          opacity: 0,
          transform: "translateX(-100%)"
        })
      ),
      transition("show => hide", animate("700ms ease-out")),
      transition("hide => show", animate("700ms ease-in"))
    ]),

    trigger("scrollAnimationSecond", [
      state(
        "show",
        style({
          opacity: 1,
          transform: "translateX(0)"
        })
      ),
      state(
        "hide",
        style({
          opacity: 0,
          transform: "translateX(100%)"
        })
      ),
      transition("show => hide", animate("700ms ease-out")),
      transition("hide => show", animate("700ms ease-in"))
    ])
  ]
})

export class AppComponent {
  state = "hide";

  constructor(public el: ElementRef) {}

  @HostListener("window:scroll", ["$event"])
  checkScroll() {
    const componentPosition = this.el.nativeElement.offsetTop;
    const scrollPosition = window.pageYOffset;

    if (scrollPosition + 700 >= componentPosition) {
     this.state = "show";
    } else {
      this.state = "hide";
    }
  }
}

time-line.component.ts:

import { Component, OnInit } from '@angular/core';
import { AppComponent } from '../app.component';

@Component({
  selector: 'app-time-line',
  templateUrl: './time-line.component.html',
  styleUrls: ['./time-line.component.css'],
})
export class TimeLineComponent implements OnInit {

  constructor() {
   }

  ngOnInit() {

  }

}

【问题讨论】:

    标签: angular typescript components angular-animations


    【解决方案1】:

    通常你设置所有的动画 在一个或多个文件中,例如:

    animation.main.ts

    export const formStateMainTrigger = trigger("scrollAnimationMain", [
      state(
        "show",
        style({
          opacity: 1,
          transform: "translateX(0)"
        })
      ),
      state(
        "hide",
        style({
          opacity: 0,
          transform: "translateX(-100%)"
        })
      ),
      transition("show => hide", animate("700ms ease-out")),
      transition("hide => show", animate("700ms ease-in"))
    ]);
    
    export const formState2Trigger = trigger("scrollAnimationSecond", [
      state(
        "show",
        style({
            opacity: 1,
            transform: "translateX(0)"
        })
      ),
      state(
        "hide",
          style({
            opacity: 0,
            transform: "translateX(100%)"
          })
        ),
        transition("show => hide", animate("700ms ease-out")),
        transition("hide => show", animate("700ms ease-in"))
    ]);
    

    你可以像这样导入它们

    import { formStateMainTrigger } from './animations.main';
    animations: [formStateMainTrigger]
    

    对于组件内部的方法,你可以这样做:

    export function checkScroll(el) {
        const componentPosition = el.nativeElement.offsetTop;
        const scrollPosition = window.pageYOffset;
        if (scrollPosition + 700 >= componentPosition) {
         return "show";
        } 
        return "hide";
    }
    

    【讨论】:

    • 感谢您的评论。那么我每次都必须在底部添加滚动功能还是可以在Animation.main.ts中添加它?
    • 你把你的动画放在一个外部文件中,然后你可以把它导入到任何你想要的地方
    • 是的,我明白了,谢谢,但在我的动画底部,我有一个在滚动上实现的功能。我可以把它留在那里还是每次都需要写?
    • 谢谢@wisher。当我尝试导入时,我确实收到一条错误消息cannot find module './animations.main'.。不知道是不是我做错了什么?
    • 我认为如果你在TimelineComponent中使用它应该是如果你已经将动画设置为与AppComponent'../animations.main'相同的级别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多