POV
POV

Reputation: 12025

How to get clientHeight in Angular?

I tried to get client height when I scroll page:

@HostListener('window:scroll', ['$event']) onScrollEvent($event) {
   if ($event.scrollHeight - $event.scrollTop === $event.clientHeight) {
    console.log('scrolled to the end');
  }
});

I need to detect if user scrolled to the end of page

Upvotes: 3

Views: 2720

Answers (2)

jo_va
jo_va

Reputation: 13993

I think you can do it like this (see this post for other solutions, and this article for this solution):

@HostListener("window:scroll") onWindowScroll() {
    let scroll = window.pageYOffset ||
                 document.documentElement.scrollTop ||
                 document.body.scrollTop || 0;

    const max = document.documentElement.scrollHeight -
                document.documentElement.clientHeight;

    if (scroll === max) {
        alert('Bottom');
    }
}

See the Stackblitz demo here

Upvotes: 3

Eric Svitok
Eric Svitok

Reputation: 842

To get the height:

 var height = $window.innerHeight;

Upvotes: 1

Related Questions