代码之家  ›  专栏  ›  技术社区  ›  Stefan Falk

窗口.scrollTo没有效果

  •  0
  • Stefan Falk  · 技术社区  · 6 年前

    为此,我得到宿主元素的y偏移量并调用 window.scrollTo() 但没有效果:

    animationDone(event) {
      window.scrollTo(0, this.host.nativeElement.getBoundingClientRect().top);
    }
    

    所以,我在这个话题上没有受过很好的教育,但是我不知道DOM中的哪个元素 window 实际代表或如何代表 scrollTo div 当前页的)。

    以下是页面外观的一个小示例:

    <!-- The scrollable content container -->
    <div class="content-container">
    
       <!-- The list items -->
    
       <div *ngFor="let element of elements>
          <div [@fullscreenAnimation] (@fullscreenAnimation)="animationDone($event)">
              <list-card [data]="element"></list-card>
          </div>
       </div>
    
    </div>
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Dan Patil    6 年前

    在HTML中提供元素ID引用

     document.getElementById('yourHTMLElementID').scrollIntoView({
              behavior: 'smooth'
         })