简体   繁体   English

从Angular中其他不同组件中的一个组件实现动画功能

[英]Implement animation function from one component in different other components in Angular

I am trying to implement an animation function which I have written in my app.component.ts into different other components. 我正在尝试实现一个动画功能,该功能已在我的app.component.ts编写为其他组件。 In effect, I would only like to have this one function and implement it in a few other components, rather than writing the function over and over again. 实际上,我只想拥有一个功能并在其他一些组件中实现它,而不是一遍又一遍地编写该功能。 I don't know if it is the right way to execute this or if there is a better way? 我不知道这是执行此操作的正确方法,还是有更好的方法?

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: 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() {

  }

}

Usually you set all your animations in one o more files like: 通常,您将所有动画设置在一个或多个文件中,例如:

animation.main.ts 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"))
]);

than you can import them like 比你可以像导入

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

For the method inside the component you could do like: 对于组件内部的方法,您可以执行以下操作:

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

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

相关问题 Angular 8 - 在其他不同的组件中使用相同的组件 - Angular 8 - Use same component in other different components 有角,在其他两个组件中使用一个组件 - Angular, using one component in other two components 如何以角度将方法从一个组件调用到其他组件(单个组件) - How to Call the method from one component to other components (individual components) in angular 如何使用 Angular 将 function 从一个组件传递到另一个组件? - How to pass a function from one component to an other using Angular? 角度4中从一个分量到另一个分量的数据 - data from one component to other component in angular 4 在具有不同组件的一个模块内实现 Angular 延迟加载 - Implement Angular Lazy Loading inside one Module with different components 如何从 Angular 中的另一个组件调用获取 API function (组件彼此不相关) - How to call a get API function from another component in Angular (Components are not related to each other ) 如何从Angular2的其他组件中调用组件中的函数? - How to call functions in a component from other components in Angular2? Angular - 如何从其他组件直接挂钩到已加载的组件实例 - Angular - How to directly hook into a loaded instance of a Component from other Components 将对话框组件中的数据发送到角度为6的其他组件 - To send data from dialog component to other components in angular 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM