自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

「實戰(zhàn)」用原生的 Intersection Observer API 實現(xiàn) Lazy Loading

開發(fā) 前端
Intersection Observer API 提供了一種異步檢測目標(biāo)元素與祖先元素或 viewport 相交情況變化的方法。

前一陣子在做一個項目的時候,因為每組數(shù)據(jù)都要先通過很龐大的計算,才把計算后的結(jié)果 Render 到頁面上,但這樣就導(dǎo)致如果單頁查出來的數(shù)據(jù)超過大概 5 筆,就會需要等待一段有感的時間,才能看到結(jié)果出現(xiàn)在畫面上。

后來為了解決這差勁用戶體驗,就使用到的標(biāo)題上說到的 Lazy Loading 來處理。簡單說就是,雖然要顯示的數(shù)據(jù)量有 10 筆,但因為一個頁面大概只能呈現(xiàn) 2 到 3 筆,那我就先計算那 2 到 3 筆數(shù)據(jù)然后顯示就好,剩下的數(shù)據(jù)等使用者往下滾再繼續(xù)顯示,這樣等待時間就不會太久。

然后身為一個前端工程師,再想到這個解法以后,當(dāng)然就是上 Github 找一個簡單又方便的組件來解決它 ,而最后找到的 vue-scroll-loader 使用起來非常容易,代碼也少少的,所以就在處理完 issue 后,看它內(nèi)部是如何實現(xiàn) Lazy Loading,于是就看到今天主要講的 Intersection Observer API 啦!

Intersection Observer API

那 Intersection Observer API 到底是什麼?為什麼它可以用來實現(xiàn) Lazy Loading 呢?以 MDN 的說法來說:

Intersection Observer API 提供了一種異步檢測目標(biāo)元素與祖先元素或 viewport 相交情況變化的方法。

簡單說的意思就是只要使用 Intersection Observer API,就能夠監(jiān)聽目標(biāo)的元素在畫面上出現(xiàn)或離開的時候,執(zhí)行你交給它的 callback 方法。下方就來看看使用的方式吧!

使用方法

首先要有簡單的 HTML 和 CSS,主要目標(biāo)就是把 div 放在往下滾才會出現(xiàn)的地方:

body {
height: 1000px;
}

.box {
width: 100px;
height: 100px;
background: #000;
position: absolute;
top: 500px;
}
<body>
<div class="box"></div>
</body>

接著我們用 Intersection Observer API 的 observe 方法,把要監(jiān)聽的 div 當(dāng)作參數(shù)傳給它,并用 callback 讓它可以在 div 出現(xiàn)和離開的時候給個消息:

const intersectionObserver = new IntersectionObserver(
() => { console.log('hi'); }
);

intersectionObserver.observe(
document.querySelector('.box')
);

執(zhí)行的結(jié)果就會像這樣子:

而 Intersection Observer API 在執(zhí)行 callback 的時候,也會給你一個 Array,Array 是所有正在監(jiān)聽的元素,我們可以從這些元素里的 isIntersecting 來判斷當(dāng)前的元素是出現(xiàn)在畫面中,還是離開畫面了:

const intersectionObserver = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
console.log('我進(jìn)來了!');
} else {
console.log('我又出去了!');
}
}
);

intersectionObserver.observe(
document.querySelector('.box')
);

執(zhí)行結(jié)果:

最后就是當(dāng)你不再需要繼續(xù)監(jiān)聽元素的時候,可以使用 unobserve 來解除監(jiān)聽,使用時就像監(jiān)聽用的 observe 一樣,給它不需要再監(jiān)聽的元素:

intersectionObserver.unobserve(
document.querySelector('.box')
);

以上就是 Intersection Observer API 的基本用法,當(dāng)然還有其他比較仔細(xì)的設(shè)置(可以看 MDN 的介紹),但如果要完成一個簡單的 Lazy Loading,那只要會上方的幾種使用方式就綽綽有馀了!

Lazy Loading

Intersection Observer API 實現(xiàn) Lazy Loading 的方法就是在數(shù)據(jù)列表的最后放個 loading 的小動畫,接著只要去監(jiān)聽小動畫,當(dāng)它出現(xiàn)在頁面中的時候,用 Intersection Observer API 的 callback 載入更多數(shù)據(jù)。

首先一樣先簡單寫個要顯示數(shù)據(jù)的<ul>和要監(jiān)聽的元素,這裡我就不做小動畫了,直接用 Loading… 文字代替 :

<body>
<ul class="list"></ul>
<div class="loading">Loading...</div>
</body>

要注意監(jiān)聽的元素必須要在載入數(shù)據(jù)的最下面哦!不然它不會被監(jiān)聽到“出現(xiàn)在頁面上”了(這個下方會更詳細(xì)說明注意事項)。

JavaScript 的部分先貼代碼,下方再來解釋:

const data = Array.from(Array(200)).map(
(_value, index) => `第 ${index + 1} 筆資料`
);

const render = () => {
const list = document.querySelector('.list');

const LOAD_DATA_COUNT = 50;
const startLoadIndex = list.childNodes.length;
const endLoadIndex = startLoadIndex + LOAD_DATA_COUNT;
for (let i = startLoadIndex; i < endLoadIndex; i++) {
if (data[i]) {
const text = document.createTextNode(data[i]);
const li = document.createElement('li');
li.appendChild(text);
list.appendChild(li);
}
}

if (endLoadIndex >= data.length) {
const loading = document.querySelector('.loading');
loading.style.display = 'none';
intersectionObserver.unobserve(loading);
}
};

render();

const intersectionObserver = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
setTimeout(render, 1000);
}
}
);

intersectionObserver.observe(
document.querySelector('.loading')
);
  1. 先用循環(huán)產(chǎn)生 200 筆的假數(shù)據(jù)
  2. 寫一個 render 的方法,把還沒載入的數(shù)據(jù)循環(huán)加去,這里一次加 50 筆數(shù)據(jù)
  3. 在 render 內(nèi)加完數(shù)據(jù),去判斷當(dāng)前加到的 index 有沒有大于數(shù)據(jù)總數(shù),如果有的話代表所有數(shù)據(jù)顯示完了,因此隱藏 loading,并移除 Intersection Observer API 對 loading 的監(jiān)聽
  4. 畢竟一開始畫面上還是要有數(shù)據(jù)!所以先手動執(zhí)行第一次 render 方法
  5. 用 Intersection Observer API 監(jiān)聽 loading,只要一出現(xiàn)在畫面上(代表使用者看完目前的數(shù)據(jù),就要在執(zhí)行 render。這裡為了有真正 render 的感覺,我用 setTimeout 來延遲 1 秒

執(zhí)行的效果就會像這樣子:

但是還有一點要注意的地方,以上方的例子來說,如果 Intersection Observer API 因為 loading 出現(xiàn)在頁面中執(zhí)行了 render,但是 render 后的數(shù)據(jù)量卻不足以把 loading 移到畫面外,那 loading 就會一直停留在畫面中,而不是“出現(xiàn)在畫面中”,這麼一來,Intersection Observer API 也就不會觸發(fā) render 載入更多數(shù)據(jù)。

最后來看一下支持情況。ntersection Observe API 的支持度算不錯了,但如果產(chǎn)品有要考慮到 IE 的客戶群就沒辦法用了。

最后還是覺得從開源項目里面以學(xué)到很多有趣的東西,也推薦大家可以在使用某些組件時候偷看一下背后的源碼怎麼處理的。

~完,我是刷碗智,新的一年,我們一起洗刷刷新!!

作者:神Q超人 譯者:前端小智 來源:medium 原文:https://medium.com/starbugs/%E7%94%A8%E5%8E%9F%E7%94%9F%E7%9A%84-javascript-intersection-observer-api-%E5%AF%AE7%8F%BE-lazy-loading-6bedccd0950

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2022-08-26 12:13:08

APIjavascript視頻

2022-03-06 20:02:21

監(jiān)聽視頻播放

2022-09-22 09:44:39

技術(shù)元素

2009-06-16 11:49:00

JQuery實現(xiàn)loaWCF

2022-10-18 07:10:09

Python應(yīng)用程序模塊

2025-03-12 11:14:45

2012-01-18 10:47:38

ibmdw

2009-03-19 15:52:50

Silverlight位圖WPF

2023-04-17 16:21:20

JavaScriot前端開發(fā)

2021-04-05 14:48:51

JavaScriptjQuery函數(shù)

2009-09-24 13:12:20

Hibernate原生

2010-02-22 14:28:35

WCF實現(xiàn)loadin

2009-07-02 09:34:05

hibernate的l

2024-05-29 09:34:02

GPTs原生API

2024-09-04 08:46:38

2023-10-08 20:32:59

CSS定義Loading

2017-04-20 13:30:00

AndroidRetrofit 2文件上傳

2022-01-13 07:04:54

CSS 技巧Loading 動畫

2023-05-22 09:10:53

CSSloading 效

2023-06-27 09:33:15

Loading 動畫CSS
點贊
收藏

51CTO技術(shù)棧公眾號