I have a sticky navigation bar and I want to click on a button and get that element to be scrolled into view.
This works however it always puts the header on top of the element I am trying to scroll into view.
html template:
<div id="arrow-wrapper">
<div (click)="onArrowClick()" class="arrow-border">
<div class="arrow down"></div>
<div class="pulse"></div>
</div>
</div>
...
<div style="padding: 0 10px;">
<h1 #gearUp [ngClass]="routeAnimationsElements" class="heading">Gear Up!</h1>
Component.ts file:
@ViewChild('gearUp') merchandiseCards: ElementRef;
onArrowClick(){
const targetELement = this.merchandiseCards.nativeElement as HTMLElement;
//targetELement.scrollIntoView({behavior: 'smooth'});
const elementPosition = targetELement.getBoundingClientRect().top;
const offsetPosition = elementPosition - 50;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
})
targetELement.scrollBy({
top: offsetPosition,
behavior: 'smooth'
})
}
In the above example the window.scrollTo or targetElement.scrollBy do not work. Only targetElement.ScrollIntoView works but this puts the header element over top of the anchor element. How should I approach this?