【问题标题】:Is there a way to scroll to the bottom of the page in angular when clicking a button?单击按钮时,有没有办法以角度滚动到页面底部?
【发布时间】:2019-10-26 22:05:48
【问题描述】:

我正在做一个有角度的项目,我需要想办法在用户单击按钮时自动滚动到页面底部。

我尝试使用对我不起作用的 jQuery 函数 animate() 来执行此操作。我也尝试过以下功能:

scrollToBottom() {
  let myElement = document.getElementById("myPageId");
  myElement.scrollTop = myElement.scrollHeight;
}

这个函数只有在我从我的html页面调用它时才有效,而当用户点击一个按钮时我需要在我的ts文件中使用它。

【问题讨论】:

标签: jquery html css angular typescript


【解决方案1】:

创建一个按钮来执行这个由@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>

完成stackblitz

【讨论】:

  • 哇.. 超级干净!不知道为什么人们会选择 ElementRef 和 ViewChild 来滚动到底部……这绝对是一个将页面向下滚动到底部的功能的解决方案。
【解决方案2】:

在这里查看答案Scroll Automatically to the Bottom of the Page 这是代码,只需将其添加到您的函数 scrollToBottom 中

window.scrollTo(0,document.body.scrollHeight);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 2022-01-04
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 2016-03-07
  • 2021-05-04
相关资源
最近更新 更多