Skip to content

Commit 9e6ebe1

Browse files
committed
fix(): use the ViewportRuler for the viewport measurements
1 parent 1b66eea commit 9e6ebe1

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

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

34
/**
45
* Utilitity that allows for toggling scrolling of the viewport on/off.
@@ -15,18 +16,23 @@ export class DisableBodyScroll {
1516
return this._isActive;
1617
}
1718

19+
constructor(private _viewportRuler: ViewportRuler) {}
20+
1821
/**
1922
* Disables scrolling if it hasn't been disabled already and if the body is scrollable.
2023
*/
2124
activate(): void {
22-
if (!this.isActive && document.body.scrollHeight > window.innerHeight) {
23-
let body = document.body;
25+
let body = document.body;
26+
let bodyHeight = body.scrollHeight;
27+
let viewportHeight = this._viewportRuler.getViewportRect().height;
28+
29+
if (!this.isActive && bodyHeight > viewportHeight) {
2430
let html = document.documentElement;
2531
let initialBodyWidth = body.clientWidth;
2632

2733
this._htmlStyles = html.style.cssText || '';
2834
this._bodyStyles = body.style.cssText || '';
29-
this._previousScrollPosition = window.scrollY || window.pageYOffset || 0;
35+
this._previousScrollPosition = this._viewportRuler.getViewportScrollPosition().top;
3036

3137
body.style.position = 'fixed';
3238
body.style.width = '100%';

0 commit comments

Comments
 (0)