简体   繁体   English

Angular 4:以编程方式滚动到页面顶部

[英]Angular 4: Scroll to top of page programmatically

Does anyone know how to use Angular 4 and programmatically scroll the page to the top? 有谁知道如何使用Angular 4并以编程方式将页面滚动到顶部? My usescase is that I have a search page, and the input is at the top, and links to the other search results pages are at the bottom. 我的用例是我有一个搜索页面,输入位于顶部,其他搜索结果页面的链接位于底部。 When the user clicks a link (2,3,4...) I would like the page to populate with the new results (functionality is complete) and then scroll to the top of the page to show the results (this functionality is not complete). 当用户单击链接(2,3,4 ...)时,我希望页面填充新结果(功能已完成),然后滚动到页面顶部以显示结果(此功能不是完成)。

I've see that this is possible with JQuery ( Scroll to top of page ) but I was wondering if there was a way to stay in the Angular 4 stack. 我已经看到这可以用JQuery( 滚动到页面顶部 ),但我想知道是否有一种方法可以留在Angular 4堆栈中。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Just use the native JavaScript window.scrollTo method -- passing in 0,0 will scroll the page to the top left instantly. 只需使用本机JavaScript window.scrollTo方法 - 传入0,0会立即将页面滚动到左上角。

window.scrollTo(xCoord, yCoord);

Parameters 参数

  • xCoord is the pixel along the horizontal axis. xCoord是沿水平轴的像素。
  • yCoord is the pixel along the vertical axis. yCoord是沿垂直轴的像素。

You don't need additional library to do this. 您不需要额外的库来执行此操作。 A standard HTML tag will be sufficient by using below syntax 使用以下语法,标准HTML标记就足够了

<div id="place">
   Something.....
</div>

<a target="#place">Click Here .....!</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM