angular8 頁面滾動到某一個特定位置


背景:使用angular8安裝的 Fuse-v8.0.0-demo 后台的框架

錨點: id='nearby'

流程:無論是點擊觸發方法也好,還是直接調用方法也好,這里只寫

 

引入:

import {Component, ViewEncapsulation, OnInit, OnDestroy, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
import {FusePerfectScrollbarDirective} from '../../../../../@fuse/directives/fuse-perfect-scrollbar/fuse-perfect-scrollbar.directive';

屬性:

@ViewChild(FusePerfectScrollbarDirective, {static: false})
directiveScroll: FusePerfectScrollbarDirective;

 

方法:

// 在頁面渲染完成時,開始執行這個方法

ngAfterViewInit(): void
{
setTimeout(() => {
this.scrollToBottom();
});
}

/**
* Scroll to the bottom
* speed是頁面滾動的速度  
* @param {number} speed
*/
scrollToBottom(speed?: number): void
{
speed = speed || 400;
if ( this.directiveScroll )
{
// this.directiveScroll.update();

setTimeout(() => {
this.directiveScroll.scrollToElement('#nearday' , 0 , speed); //頁面滾動到nearday的地方
});
}
}


頁面渲染部分:
在html頁面中滾動的那個div加上 放上屬性
fusePerfectScrollbar

<div 這里外層的div要overflow:hidden>
  <div fusePerfectScrollbar 這里是滾動的部分>
    <section>這里是內容</section>
  </div>
</div>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM