单击一个按钮(这是页面的底部),我想转到当前页面顶部的某个元素(在我的例子中是#navbar),但是我不知道该如何做。我尝试了下面的代码,但没有结果。
<nav class="navbar navbar-light bg-faded" id="navbar">
<a class="navbar-brand" href="#">
{{appTitle}}
</a>
<!-- rest of the nav link -->
</nav>
<!-- rest of the page content -->
<!-- bottom of the page -->
<button class="btn btn-outline-secondary" (click)="gotoTop()">Top</button>在角部分:
import { Router } from '@angular/router';
/* rest of the import statements */
export class MyComponent {
/* rest of the component code */
gotoTop(){
this.router.navigate([], { fragment: 'navbar' });
}
}如果有人帮助我找到解决方案并解释为什么我的代码不起作用,我会非常感激的。
请注意元素(navbar)在其他组件中。
发布于 2017-08-18 18:05:56
我知道,您希望滚动到页面中的特定元素。但是,如果元素位于页面顶部,则可以使用以下方法:
window.scrollTo(0, 0);https://stackoverflow.com/questions/45742611
复制相似问题