Angular中使用Intersection Observer API實現(xiàn)無限滾動效果
背景:
實現(xiàn)原理為 在data下面加一個loading元素 如果此元素進入視窗 則調(diào)用api獲取新的數(shù)據(jù)加到原來的數(shù)據(jù)里面,這時loading就會被新數(shù)據(jù)頂下去,如此循環(huán)。
<div id="dataContainer"></div> <div id="loadingContainer"></div>
傳統(tǒng)angular實現(xiàn)是使用ngAfterViewInit()生命周期,寫在指令(Directive)里面,并且傳出一個事件,觸發(fā)時回調(diào)被監(jiān)控組件里面的具體函數(shù)。
不過對于異步操作,元素可能在ngAfterViewInit被調(diào)用時還沒有完成初始化而導(dǎo)致bug,所以用ngAfterViewChecked() 會更穩(wěn),當然也更會導(dǎo)致性能問題,每次變更檢測它都被調(diào)用,這可能會增加應(yīng)用程序的負載。
所以這里使用h5 提供的新API ——交叉觀察器 API(Intersection Observer API)提供了一種異步檢測目標元素與祖先元素或頂級文檔的視口相交情況變化的方法。
效果如圖,為了截到loading,我在增加數(shù)據(jù)的函數(shù)里面加了個等待。
上代碼
watch-to-view.directive.ts
import { Directive, Output,EventEmitter, ElementRef } from '@angular/core'; @Directive({ selector: '[appWatchToView]' }) export class WatchToViewDirective { private observer:IntersectionObserver; @Output() appWatchToView = new EventEmitter<IntersectionObserverEntry>(); constructor(public el:ElementRef) { this.observer = new IntersectionObserver(this.callback,{rootMargin:'100px',threshold:1,root:null}); this.observer.observe(el.nativeElement); } public callback = (entries: IntersectionObserverEntry[]) => { entries.forEach((entry: IntersectionObserverEntry) => { if (entry.isIntersecting) { this.appWatchToView.emit(entry); } }) } }
app.component.html
<div id="dataContainer"> <ul> <li class="blueBox" *ngFor="let i of data">{{i}}</li> </ul> </div> <div id="loadingContainer" class="blueBox" appWatchToView (appWatchToView)="this.loadObserver($event)">Loading more...</div>
app.component.ts
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Tour of Herors'; batch:Number = 10; len:Number = this.batch; data:Array<Number>=new Array(this.batch).fill(0).map((_,i)=>i); loadObserver = async function (event){ console.log(event.target); await new Promise(resolve => setTimeout(resolve, 1000)); this.data.push(...new Array(this.batch).fill(0).map((_,i)=>i+this.len)); this.len+=this.batch; } }
再給個樣式吧
.blueBox{ display: block; width: 100%; height: 100px; background-color: #38b1ee; margin-top: 5px; text-align: center; color: white; font-size: large; }
官方link: https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API
到此這篇關(guān)于Angular中使用Intersection Observer API實現(xiàn)無限滾動的文章就介紹到這了,更多相關(guān)Angular無限滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法
本篇文章主要介紹了詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法,非常具有實用價值,需要的朋友可以參考下2017-05-05AngularJS模態(tài)框模板ngDialog的使用詳解
這篇文章主要介紹了AngularJS模態(tài)框模板ngDialog的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05使用Angular 6創(chuàng)建各種動畫效果的方法
Angular能夠讓我們創(chuàng)建出具有原生表現(xiàn)效果的動畫。我們將通過本文學(xué)習到如何使用Angular 6來創(chuàng)建各種動畫效果。在此,我們將使用Visual Studio Code來進行示例演示。感興趣的朋友跟隨小編一起看看吧2018-10-10