1
1
import { Injectable , Optional , SkipSelf } from '@angular/core' ;
2
+ import { ViewportRuler } from './position/viewport-ruler' ;
2
3
3
4
4
5
/**
@@ -16,18 +17,23 @@ export class DisableBodyScroll {
16
17
return this . _isActive ;
17
18
}
18
19
20
+ constructor ( private _viewportRuler : ViewportRuler ) { }
21
+
19
22
/**
20
23
* Disables scrolling if it hasn't been disabled already and if the body is scrollable.
21
24
*/
22
25
activate ( ) : void {
23
- if ( ! this . isActive && document . body . scrollHeight > window . innerHeight ) {
24
- let body = document . body ;
26
+ let body = document . body ;
27
+ let bodyHeight = body . scrollHeight ;
28
+ let viewportHeight = this . _viewportRuler . getViewportRect ( ) . height ;
29
+
30
+ if ( ! this . isActive && bodyHeight > viewportHeight ) {
25
31
let html = document . documentElement ;
26
32
let initialBodyWidth = body . clientWidth ;
27
33
28
34
this . _htmlStyles = html . style . cssText || '' ;
29
35
this . _bodyStyles = body . style . cssText || '' ;
30
- this . _previousScrollPosition = window . scrollY || window . pageYOffset || 0 ;
36
+ this . _previousScrollPosition = this . _viewportRuler . getViewportScrollPosition ( ) . top ;
31
37
32
38
body . style . position = 'fixed' ;
33
39
body . style . width = '100%' ;
@@ -57,7 +63,7 @@ export class DisableBodyScroll {
57
63
}
58
64
59
65
export function DISABLE_BODY_SCROLL_PROVIDER_FACTORY ( parentDispatcher : DisableBodyScroll ) {
60
- return parentDispatcher || new DisableBodyScroll ( ) ;
66
+ return parentDispatcher || new DisableBodyScroll ( new ViewportRuler ( ) ) ;
61
67
} ;
62
68
63
69
export const DISABLE_BODY_SCROLL_PROVIDER = {
0 commit comments