Angular 4 - how to trigger an animation when a div comes into the viewport?

后端 未结 5 1237
误落风尘
误落风尘 2020-12-08 17:26

I\'ve been building a new site using Angular 4 and i\'m trying to re-create a effect where when a div becomes visible (when you scroll down the screen) then that can then tr

5条回答
  •  不思量自难忘°
    2020-12-08 18:05

    Here is a simple example of an infinity scroll; it triggers handleScrollEvent() when the element comes inside the viewport.

    inside item-grid.component.html

    Load more
    

    inside item-grid.component.ts:

    @ViewChild('loadmoreBtn') loadmoreBtn: ElementRef;
    curpage: number;
    maxpage: number;
    
    ngOnInit() {
      this.curpage = 1;
      this.maxpage = 5;
    }
    
    handleScrollEvent() {
      const { x, y } = this.loadmoreBtn.nativeElement.getBoundingClientRect();
      if (y < window.innerHeight && this.maxpage > this.curpage) {
        this.curpage++;
      }
    }
    

提交回复
热议问题