我想知道为什么以下几点是有效的:
scroll(){
window.scrollTo(0, this.ypos); // works perfectly fine
}在我的html.component中:
<button (click)="scroll()">Scroll</button>但以下几点是行不通的:
ngAfterViewInit(){
console.log(this.ypos); // is perfectly defined
window.scrollTo(0, this.ypos); // won't work
}有人知道为什么它不起作用吗?window.scrollTo()也不适用于ngOnInit,但为什么?
发布于 2020-07-10 08:11:30
而不是使用窗口对象,使用角视图滚筒服务滚动到指定的位置。
component.ts
import { ViewportScroller } from '@angular/common';
constructor(private viewportScroller: ViewportScroller) {}
ngAfterViewInit(){
this.viewportScroller.scrollToPosition([0,this.ypos]);
}https://stackoverflow.com/questions/62829479
复制相似问题