亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在視口中時的 Angular 9 動畫

在視口中時的 Angular 9 動畫

一只名叫tom的貓 2022-12-22 13:04:06
我想為我的項目引入動畫,特別是<section>一些標題(h1、h2、h3)。我嘗試了幾種選擇;一種使用Angular動畫,另一種使用animate.css。這兩個都按預期工作,但現在我只想<section>在 a當前處于視圖中時(第一次)設置動畫。起初我嘗試了https://www.npmjs.com/package/ng2-animate-on-scroll,但我無法讓它工作。即使使用animate.scss。然后我嘗試了:https ://scrollrevealjs.org/通過使用https://www.npmjs.com/package/ngx-scrollreveal。這確實有效,但我只能讓它使用cubic-bezier(0.25, 0.1, 0.25, 1). 其他似乎都不起作用,我想擁有animate.css或至少fadeInUp、fadeInLeft和fadeInRight中可用的所有功能然后我嘗試了:https ://github.com/Epenance/ngx-animate-in#readme再次,有效并且是迄今為止最好的,因為它使用角度動畫,但在 IE 中根本不支持,所以這是不好。所以,我的問題是:有更好的方法嗎?理想情況下,我希望在將內容滾動到視圖中時使用角度動畫,并且我希望能夠控制要使用的動畫。這可能嗎?有沒有做過或使用過任何可能有幫助的東西?
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

最后,使用了一些我將其與 ngx-animate 指令合并的舊代碼來得出這個:


import { Directive, ElementRef, HostListener, Input } from '@angular/core';

import {

  AnimationBuilder,

  AnimationFactory,

  AnimationMetadata,

  AnimationPlayer,

  style,

  animate,

} from '@angular/animations';


@Directive({

  selector: '[sxpAnimate]',

})

export class AnimateDirective {

  @Input() animateInAnimation: AnimationMetadata | AnimationMetadata[];

  @HostListener('window:scroll', ['$event']) // for window scroll events

  onScroll() {

    this.animate();

  }


  private animating: boolean;

  private player: AnimationPlayer;

  private defaults: any = {

    offset: 0,

  };


  constructor(

    private el: ElementRef,

    private animationBuilder: AnimationBuilder

  ) {}


  ngOnInit() {

    this.initialize();

    this.animate();

  }


  private initialize(): void {

    let animation: AnimationFactory;


    if (

      this.animateInAnimation !== null &&

      this.animateInAnimation !== undefined

    ) {

      animation = this.animationBuilder.build(this.animateInAnimation);

    } else {

      animation = this.animationBuilder.build([

        style({ opacity: 0, transform: 'translateX(-100px)' }),

        animate(

          '1200ms cubic-bezier(0.35, 0, 0.25, 1)',

          style({ opacity: 1, transform: 'translateX(0)' })

        ),

      ]);

    }


    this.player = animation.create(this.el.nativeElement);

    this.player.init();

  }


  private animate(): void {

    const inView = this.isInViewport();


    if (!inView) this.animating = false;

    if (!inView || this.animating) return;


    this.player.play();

    this.animating = true;

  }


  private isInViewport(): boolean {

    const bounding = this.el.nativeElement.getBoundingClientRect();


    let top =

      bounding.top -

      (window.innerHeight || document.documentElement.clientHeight);

    let bottom = bounding.top + bounding.height + this.defaults.offset;


    return top < 0 && bottom > 0;

  }

}

似乎按我的意愿工作;所以我會以此為基礎:)


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號