fix(common): Prefer to use pageXOffset / pageYOffset instance of scro… · angular/angular@5692607 · GitHub
Skip to content

Commit 5692607

Browse files
wKozaAndrewKushnir
authored andcommitted
fix(common): Prefer to use pageXOffset / pageYOffset instance of scrollX / scrollY (#28262)
This fix ensures a better cross-browser compatibility. This fix has been used for angular.io. PR Close #28262
1 parent 2506c02 commit 5692607

4 files changed

Lines changed: 13 additions & 15 deletions

File tree

aio/src/app/shared/scroll.service.ts

Lines changed: 1 addition & 1 deletion

packages/common/src/viewport_scroller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class BrowserViewportScroller implements ViewportScroller {
8989
*/
9090
getScrollPosition(): [number, number] {
9191
if (this.supportsScrolling()) {
92-
return [this.window.scrollX, this.window.scrollY];
92+
return [this.window.pageXOffset, this.window.pageYOffset];
9393
} else {
9494
return [0, 0];
9595
}
@@ -149,7 +149,7 @@ export class BrowserViewportScroller implements ViewportScroller {
149149
*/
150150
private supportScrollRestoration(): boolean {
151151
try {
152-
if (!this.window || !this.window.scrollTo) {
152+
if (!this.supportsScrolling()) {
153153
return false;
154154
}
155155
// The `scrollRestoration` property could be on the `history` instance or its prototype.
@@ -166,7 +166,7 @@ export class BrowserViewportScroller implements ViewportScroller {
166166

167167
private supportsScrolling(): boolean {
168168
try {
169-
return !!this.window.scrollTo;
169+
return !!this.window && !!this.window.scrollTo && 'pageXOffset' in this.window;
170170
} catch {
171171
return false;
172172
}

packages/common/test/viewport_scroller_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ describe('BrowserViewportScroller', () => {
1515
let windowSpy: any;
1616

1717
beforeEach(() => {
18-
windowSpy = jasmine.createSpyObj('window', ['history', 'scrollTo']);
18+
windowSpy =
19+
jasmine.createSpyObj('window', ['history', 'scrollTo', 'pageXOffset', 'pageYOffset']);
1920
windowSpy.history.scrollRestoration = 'auto';
2021
documentSpy = jasmine.createSpyObj('document', ['getElementById', 'getElementsByName']);
2122
scroller = new BrowserViewportScroller(documentSpy, windowSpy, null!);

packages/router/test/bootstrap.spec.ts

Lines changed: 7 additions & 10 deletions

0 commit comments

Comments
 (0)