Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to specific div on (click) in Angular

I have a #content div and a button component.

When I click the button, I want to scroll to the top of the #content div.


HTML:

<div id="content">
    Loren ipsum 
</div>
<div (click)="toTop($event)">top</div>

topscroll.component.ts:

export class TopscrollComponent implements OnInit { 
  constructor() { } 

  ngOnInit() { }

  toTop(event){ 
    ...
  }
}
like image 876
Apple Orange Avatar asked Dec 03 '22 19:12

Apple Orange


1 Answers

You can do that with plain javascript using .scrollIntoView():

toTop() {
  document.getElementById("content").scrollIntoView();
}

Note: you don't need event.

like image 96
Jeffrey Roosendaal Avatar answered Dec 06 '22 10:12

Jeffrey Roosendaal