【问题标题】:scrollTop not working after adding ngAfterViewChecked添加 ngAfterViewChecked 后,scrollTop 不起作用
【发布时间】:2020-07-01 11:18:02
【问题描述】:

我需要添加 MovetoTop 和 MovetoBottom 按钮

我还需要在页面加载时自动 MovetoBottom

调用scrollToBottom函数ngAfterViewChecked后代码不工作。(只有scrollToBottom函数工作,scrollToTop函数不工作)。

请帮忙

我在这里分享我的代码。

打字稿

import { Component, OnInit, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
import { Router  } from '@angular/router';

@Component({
  selector: 'app-review',
  templateUrl: './review.component.html',
  styleUrls: ['./review.component.scss']
})
export class ReviewComponent implements OnInit  {
  @ViewChild('scrollMe', { read: ElementRef }) public scroll: ElementRef;




  ngAfterViewChecked() {        
     this.scrollToBottom();        
  }

  public scrollToBottom() {
    console.log(this.scroll.nativeElement.scrollTop);
    this.scroll.nativeElement.scrollTop = this.scroll.nativeElement.scrollHeight;

  }

  public scrollToTop() {
    this.scroll.nativeElement.scrollTop = 0;
  }


}

HTML

<div #scrollMe style="overflow: scroll; overflow-x: hidden;height:100%;">
 Hai
</div>

【问题讨论】:

  • i need add to top, add to bottom buttons also i need automatically move to bottom when the page loading。这种说法令人困惑。
  • 检查this是否适合您

标签: angular scrolltop


【解决方案1】:

scrollToTop()函数中试试这个..

public scrollToTop() {
  this.scroll.nativeElement.scrollTop(0);
}

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2015-11-23
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多