作者:手机用户2502895987 | 来源:互联网 | 2023-02-05 12:12
我正在开发angular2 web应用程序,我需要以下帮助.我的页面由多个组件组成.我想在用户点击按钮时滚动页面顶部.我试过
document.body.scrollTop = 0;
但这不适用于Chrome.我试过document.documentElement.scrollTop = 0; window.scrollTo(0,0); 但没有工作
1> 小智..:
像这样导入,
import { Inject} from "@angular/core";
import { DOCUMENT } from '@angular/platform-browser';
在你的构造函数中添加这个,
constructor(@Inject(DOCUMENT) private document: Document) { }
然后你可以像这样设置滚动,
this.document.body.scrollTop = 0;
从@ angular/platform-browser导入似乎已被弃用,建议从@ angular/common导入.[链接](https://angular.io/api/platform-browser/DOCUMENT)
这似乎不起作用.没有错误,只是没有响应,使用Angular 4/CLI.
2> 小智..:
我使用数据绑定解决了角度滚动问题:
...
与样式:
.container {
height: 100%;
scroll: auto;
position: relative;
}