【问题标题】:Scroll to specific div on (click) in Angular在 Angular 中滚动到(单击)上的特定 div
【发布时间】:2018-07-06 14:46:31
【问题描述】:

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

当我单击按钮时,我想滚动到#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){ 
    ...
  }
}

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

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

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

    注意:你不需要event

    【讨论】:

    • 我想设置特定的 id="content"...因为我只为那个 id 写了滚动 css
    • 啊,你想滚动到#content div的顶部吗?
    • 是的..我期待那个
    • console.log(top) 说什么?它是未定义的,还是一个数字?
    • 只获取号码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    相关资源
    最近更新 更多