Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  159] [ 2]  / answers: 1 / hits: 17444  / 6 Years ago, mon, june 4, 2018, 12:00:00

I have a menu bar with 4 tabs and when I press on every tab it should scroll down to the corresponding category (a specific id), on the same page.
This is the code for the menu:



  template: `
<div class=action-container>
<div class=btn-group quick-navigation custom-group role=group>
<a *ngFor=let section of sections [scrollTo]=section.id (click)=active = section.id
class=btn btn-secondary [class.active]=section.id == active>{{section.name}}</a>
</div>
</div>
`,


And this one is the scroll directive:



 @Directive({
selector: '[scrollTo]'
})
export class ScrollToDirective {
@Input() scrollTo: string;
@HostListener('click', ['$event']) onClick(e) {
console.log(this.scrollTo);
console.log($(`#${this.scrollTo}`).offset().top - 150);
$('html, body').animate({
scrollTop: $(`#${this.scrollTo}`).offset().top - 150
}, 500);
}
}


The problem is that the offset().top-150 of a specific element changes after switching tabs multiple times and the first time it will redirect to the correct element, but after that, the page will jump randomly to different elements.



I have attached a print screen of the console.



console screenshot



Any suggestions? Thank you!


More From » scrollto

 Answers
154

You don't have to compute any value to do what you want, and don't have to use JavaScript at all. At least, as you don't want a smooth scrolling.



You can easily scroll to any element with it id setting the href attribute :



<a href=#my_id id=my_link>Click me to scroll down</a>

...

<section id=my_id>...</section>


Ok, according to your comment, there is a conflict with the angular's Router when doing this, so with a little bit of JavaScript you can solve the problem :



document.getElementById(my_link).addEventListener(click,(e)=>{
e.preventDefault();
document.querySelector(e.currentTarget.href).scrollIntoView();
});

[#54278] Thursday, May 31, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sylviakaitlinj

Total Points: 564
Total Questions: 114
Total Answers: 105

Location: Jordan
Member since Thu, Aug 11, 2022
2 Years ago
;