如何在overflow-y div中强制滚动

问题描述 投票:0回答:1

我正在尝试强制包含ngfor列表的div中的偏移滚动

我在div上尝试了这个溢出-y:

@ViewChild('list') listRef: ElementRef;

然后单击我尝试使用一些日志,以确保它被调用

this.listRef.nativeElement.offsetTop = 500;

什么都没发生,任何想法我怎么能做到这一点?

编辑:

Html:

<div
  #gridContainerId
  class="gridContainer">
  <a *ngFor="let item of images;">
     <img src="{{getImage(item)}}"/>
  </a>
</div>

css:

.gridContainer {
  height: 90vh;
  overflow-y: scroll;
}

零件 :

@ViewChild('gridContainerId') ref: ElementRef;

this.store.pipe(
      select(fromImages.getImages),
      takeWhile(() => this.componentActive)
    ).subscribe(imagesItems => {
        this.images = imagesItems;
        updateScroll();
      }
    );

updateScroll(){
  this.ref.nativeElement.scrollTop = this.ref.nativeElement.scrollHeight;
}
javascript html angular typescript ngfor
1个回答
0
投票

我最近在同一个场景中,当点击某个按钮时,div滚动到底部,我用这段代码实现了它:

this.messagesContainer.nativeElement.scrollTop = this.messagesContainer.nativeElement.scrollHeight;

属性scrollTop用于指定要从视图顶部滚动的像素数。 和属性scrollHeight是获取div的总高度。

© www.soinside.com 2019 - 2024. All rights reserved.