How to detect scroll to bottom of html element

后端 未结 4 894
一生所求
一生所求 2020-12-02 11:42

I have this element that I\'m referencing by Id:

    let infiniteScrollElement = document.getElementById(\'th-infinite-scroll-tracker\');

I

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 11:54

    I think that all you want to do is detect the position of scroll.

    @HostListener("window:scroll", ["$event"])
    onWindowScroll() {
    //In chrome and some browser scroll is given to body tag
    let pos = (document.documentElement.scrollTop || document.body.scrollTop) + document.documentElement.offsetHeight;
    let max = document.documentElement.scrollHeight;
    // pos/max will give you the distance between scroll bottom and and bottom of screen in percentage.
     if(pos == max )   {
     //Do your action here
     }
    }
    

    Also don't forget to import HostListener from @angular/core.

提交回复
热议问题