javascript - גלול אל div מסוים ב-(click) ב-Angular


יש לי #content div ורכיב כפתור.

כאשר אני לוחץ על הכפתור, אני רוצה לגלול למעלה של הדיב #content.

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){ 
    ...
  }
}

תשובה #1

אתה יכול לעשות זאת עם ג'אווהסקריפט פשוט בעזרת .scrollIntoView():

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

הערה: אינך צריך event.