@@ -13,28 +13,31 @@ export class ScrollHelperService {
1313
1414 private window : WindowProxy ;
1515 private DEFAULT_SCROLLSPEED = 50 ;
16+ private SCROLL_BUFFER = 50 ;
1617
1718 constructor ( @Inject ( DOCUMENT ) private document ) {
1819 this . window = document . defaultView ;
1920 }
2021
21- public scrollIfNecessary ( element : HTMLElement , scrollPoints : ScrollPoints = { } , scrollSpeed ?: number ) : void {
22- const bounding = element . getBoundingClientRect ( ) ;
23- if ( this . isTopScrollNeeded ( bounding . top , scrollPoints . top ) ) {
22+ public scrollIfNecessary ( event : any , scrollPoints : ScrollPoints = { } , scrollSpeed ?: number ) : void {
23+ const position = event . pageY - this . window . scrollY ;
24+
25+ if ( this . isTopScrollNeeded ( position , scrollPoints . top ) ) {
2426 this . window . scrollBy ( { top : - scrollSpeed || - this . DEFAULT_SCROLLSPEED , behavior : 'smooth' } ) ;
2527 return ;
2628 }
2729
28- if ( this . isBottomScrollNeeded ( bounding . top , scrollPoints . bottom ) ) {
30+ if ( this . isBottomScrollNeeded ( position , scrollPoints . bottom ) ) {
2931 this . window . scrollBy ( { top : scrollSpeed || this . DEFAULT_SCROLLSPEED , behavior : 'smooth' } ) ;
3032 }
3133 }
3234
33- private isTopScrollNeeded ( topBounding : number , scrollPointTop : number ) : boolean {
34- return scrollPointTop ? topBounding < scrollPointTop : topBounding < 0 ;
35+ private isTopScrollNeeded ( currentPosition : number , scrollPointTop : number ) : boolean {
36+ return scrollPointTop ? currentPosition < scrollPointTop + this . SCROLL_BUFFER : currentPosition < 20 ;
3537 }
3638
37- private isBottomScrollNeeded ( bottomBounding : number , scrollPointBottom : number ) : boolean {
38- return scrollPointBottom ? bottomBounding < scrollPointBottom : bottomBounding > this . window . innerHeight ;
39+ private isBottomScrollNeeded ( currentPosition : number , scrollPointBottom : number ) : boolean {
40+ return scrollPointBottom ?
41+ currentPosition < scrollPointBottom : currentPosition > this . window . innerHeight - this . SCROLL_BUFFER ;
3942 }
4043}
0 commit comments