Scroll to element on click in Angular 4

后端 未结 12 1012
逝去的感伤
逝去的感伤 2020-11-29 18:57

I want to be able to scroll to a target when a button is pressed. I was thinking something like this.

12条回答
  •  渐次进展
    2020-11-29 19:29

    You can scroll to any element ref on your view by using the code block below. Note that the target (elementref id) could be on any valid html tag.

    On the view(html file)

    on the .ts file,

    scroll() {
       document.querySelector('#target').scrollIntoView({ behavior: 'smooth', block: 'center' });
    }
    

提交回复
热议问题