我正在做一个 Angular 元素,我需要想出一种方法来在用户单击按钮时自动滚动到页面底部。
我尝试使用 jQuery 函数 animate() 执行此操作,但它对我不起作用。我还尝试了以下功能:
scrollToBottom() {
let myElement = document.getElementById("myPageId");
myElement.scrollTop = myElement.scrollHeight;
}
此函数仅在我从我的 html 页面调用它时有效,而当用户单击按钮时我需要在我的 ts 文件中使用它。
最佳答案
创建一个按钮来执行由@chestas 和available here 编写的普通JavaScript
相关TS:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
goToBottom(){
window.scrollTo(0,document.body.scrollHeight);
}
}
相关HTML:
<button type="button" (click)="goToBottom()">go to bottom </button>
关于jquery - 单击按钮时是否可以按 Angular 滚动到页面底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56561400/