【发布时间】:2018-11-07 01:37:00
【问题描述】:
当路线更改时,我试图滚动到我的 angular 2 站点上的页面顶部,我尝试了以下操作,但没有任何反应,当我将路线从一页更改为另一页时,页面滚动到它在第一页的位置:
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'my-app',
template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0)
});
}
}
我做错了什么?
【问题讨论】:
-
试试
setTimeout(function () {window.scrollTo(0, 300);},2) -
不,这不起作用,我的控制台日志中没有错误。
-
我试过它对我有用。其他选项是使用 jquery
$('#content').animate({ scrollTop: 20 }, 200)。 -
还是不行,烦人
-
你能提供一个stackblitz来显示这个问题吗?
标签: javascript jquery angular