I'm trying to show a component when first load the page with lazy loading that only load the content if it's in the view. For example: - There are 10 components on the page and I want to show/scroll to the component number 7 on first load with lazy loading(for performance).
How do I do this correctly? Challenge here is because these components are lazy loading and have huge images that messed up the scrollIntoView() and scrolled too much to the top passed the component.
Something like this that I tried but the component still scroll too much to the top.
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
<div>Div 5</div>
<div>Div 6</div>
<div #target>Target Div 7</div>
<div>Div 8</div>
<div>Div 9</div>
<div>Div 10</div>
export class BBQComponent implements AfterViewInit {
@ViewChild("target") targetElement: ElementRef;
ngAfterViewInit() {
setTimeout(_ => {
this.targetElement.nativeElement.scrollIntoView({block: "start"});
window.scrollBy(0, -100);
}, 2000);
}
}
I expect the page to show the component on first visit just below the navigation bar (about 100px in height). I have searched for the solutions and tried out different things but still stuck at this.
Is there something that I missed to get this feature scrollIntoView to work with lazy loading content? Thanks!!
export class BBQComponent implements AfterContentChecked {
@ViewChild("target") targetElement: ElementRef;
scrolled = false; // To avoid multiple scrolls because ngAfterContentChecked
ngAfterContentChecked() { // Changed from ngAfterViewInit()
if(!scrolled) {
const target = this.targetElement.nativeElement;
target.scrollIntoView({block: "start"}); // Scroll to the component
// After scrolled, wait for the browser to figure out where the
// component is after lazy loading is done. Scroll again.
setTimeout(_ => {
target.scrollIntoView({block: "start"});
window.scrollBy(0, -100); // Nav's height
this.scrolled = true;
}, 1000); // Adjust wait time as needed
}
}
}