Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 819
OPV Avatar asked May 13 '26 05:05

OPV


2 Answers

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

like image 65
jo_va Avatar answered May 14 '26 19:05

jo_va


To get the height:

 var height = $window.innerHeight;
like image 28
Eric Svitok Avatar answered May 14 '26 20:05

Eric Svitok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!