Skip to content

Commit

Permalink
fix(common): Prefer to use pageXOffset / pageYOffset instance of scro…
Browse files Browse the repository at this point in the history
…llX / scrollY.

This fix ensures a better cross-browser compatibility. This fix has been used for angular.io.
  • Loading branch information
wKoza committed Nov 11, 2020
1 parent 39e8dbc commit 1371940
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aio/src/app/shared/scroll.service.ts
Expand Up @@ -24,7 +24,7 @@ export class ScrollService implements OnDestroy {
poppedStateScrollPosition: ScrollPosition|null = null;
// Whether the browser supports the necessary features for manual scroll restoration.
supportManualScrollRestoration: boolean = !!window && ('scrollTo' in window) &&
('scrollX' in window) && ('scrollY' in window) && isScrollRestorationWritable();
('pageXOffset' in window) && ('pageYOffset' in window) && isScrollRestorationWritable();

// Offset from the top of the document to bottom of any static elements
// at the top (e.g. toolbar) + some margin
Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/viewport_scroller.ts
Expand Up @@ -86,7 +86,7 @@ export class BrowserViewportScroller implements ViewportScroller {
*/
getScrollPosition(): [number, number] {
if (this.supportsScrolling()) {
return [this.window.scrollX, this.window.scrollY];
return [this.window.pageXOffset, this.window.pageYOffset];
} else {
return [0, 0];
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export class BrowserViewportScroller implements ViewportScroller {
*/
private supportScrollRestoration(): boolean {
try {
if (!this.window || !this.window.scrollTo) {
if (!this.supportsScrolling()) {
return false;
}
// The `scrollRestoration` property could be on the `history` instance or its prototype.
Expand All @@ -163,7 +163,8 @@ export class BrowserViewportScroller implements ViewportScroller {

private supportsScrolling(): boolean {
try {
return !!this.window.scrollTo;
return !!this.window && !!this.window.scrollTo && !!this.window.pageXOffset &&
!!this.window.pageYOffset;
} catch {
return false;
}
Expand Down

0 comments on commit 1371940

Please sign in to comment.