Skip to content

Commit 46af646

Browse files
committed
fix(): use the ViewportRuler for the viewport measurements
1 parent fb6f382 commit 46af646

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/lib/core/overlay/disable-body-scroll.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Injectable, Optional, SkipSelf} from '@angular/core';
2+
import {ViewportRuler} from './position/viewport-ruler';
23

34

45
/**
@@ -16,18 +17,23 @@ export class DisableBodyScroll {
1617
return this._isActive;
1718
}
1819

20+
constructor(private _viewportRuler: ViewportRuler) {}
21+
1922
/**
2023
* Disables scrolling if it hasn't been disabled already and if the body is scrollable.
2124
*/
2225
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) {
2531
let html = document.documentElement;
2632
let initialBodyWidth = body.clientWidth;
2733

2834
this._htmlStyles = html.style.cssText || '';
2935
this._bodyStyles = body.style.cssText || '';
30-
this._previousScrollPosition = window.scrollY || window.pageYOffset || 0;
36+
this._previousScrollPosition = this._viewportRuler.getViewportScrollPosition().top;
3137

3238
body.style.position = 'fixed';
3339
body.style.width = '100%';
@@ -57,7 +63,7 @@ export class DisableBodyScroll {
5763
}
5864

5965
export function DISABLE_BODY_SCROLL_PROVIDER_FACTORY(parentDispatcher: DisableBodyScroll) {
60-
return parentDispatcher || new DisableBodyScroll();
66+
return parentDispatcher || new DisableBodyScroll(new ViewportRuler());
6167
};
6268

6369
export const DISABLE_BODY_SCROLL_PROVIDER = {

0 commit comments

Comments
 (0)