I am displaying a large image contained in the template of a child component through the template of a parent component. Upon loading the view of my parent component, I would like the window of my browser to scroll to a specified position to display the center of that image.
At the moment, 1) upon loading the parent page for the first time : my window does not scroll, 2) upon refreshing the page : my window does not scroll and 3) upon returning to the home screen of my app and going back to the parent component : my window scrolls as wished.
How can I make the window scroll systematically on first loading of the page ?
Thank you all in advance !
Code samples
parent.component.ts
import { AfterViewChecked, Component } from "@angular/core";
@Component({
selector: "app-parent",
templateUrl: "./parent.component.html",
styleUrls: ["./parent.component.css"]
})
export class ParentComponent implements AfterViewChecked {
ngAfterViewChecked() {
window.scrollTo(1900, 840);
}
}
parent.component.html
<app-child></app-child>
child.component.ts
import { Component } from "@angular/core";
@Component({
selector: "app-child",
templateUrl: "./child.component.html",
styleUrls: ["./child.component.css"]
})
export class ChildComponent {}
child.component.html
<!-- link points to a non-specific 3840 x 2606 jpg image -->
<img src="https://i.imgur.com/5FpYloV.jpg" />
EDIT
I decided to use a free dragging directive on my main HTML element instead of scrolling around it. I find the dragging more user-friendly that having to wait for the scrolling to end.