Jun An
Jun An

Reputation: 123

How to check that scroll is end of bottom in Angular 4?

  @HostListener('window:scroll', ['$event'])
  onWindowScroll() {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        console.log('reached bottom');
    }
  }

It is working of this code as above, but it occured many times even not reached exactly end of bottom.

How to check wheather scroll is reached to end of bottom?

Upvotes: 3

Views: 7668

Answers (2)

Ankur Singh
Ankur Singh

Reputation: 157

This worked for me.

import { HostListener } from '@angular/core';
@HostListener('window:scroll', ['$event'])
  onWindowScroll(event) {
    // 200 is the height from bottom from where you want to trigger the infintie scroll, which can also be zero to detect bottom of window
    if ((document.body.clientHeight + window.scrollY + 200) >= document.body.scrollHeight) {
        console.log('triggred');
    }
  }

Upvotes: 5

Jun An
Jun An

Reputation: 123

 if (window.innerHeight + window.scrollY === document.body.scrollHeight) {
      console.log('bottom');
 }

I found it.

Upvotes: 6

Related Questions