在Angular中(单击)滚动到特定的div [英] Scroll to specific div on (click) in Angular

查看:121
本文介绍了在Angular中(单击)滚动到特定的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个#content div和一个按钮组件.

I have a #content div and a button component.

当我单击按钮时,我想滚动到#content div的顶部.

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:

topscroll.component.ts:

export class TopscrollComponent implements OnInit { 
  constructor() { } 

  ngOnInit() { }

  toTop(event){ 
    ...
  }
}

推荐答案

您可以使用.scrollIntoView()使用普通的javascript来做到这一点:

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

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


注意:您不需要event.


Note: you don't need event.

这篇关于在Angular中(单击)滚动到特定的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆