JS實現(xiàn)滾動條觸底加載更多
原理
1.通過監(jiān)聽滾動區(qū)域DOM的scroll事件, 計算出觸底
// 滾動可視區(qū)域高度 + 當前滾動位置 === 整個滾動高度
scrollDom.clientHeight + scrollDom.scrollTop === scrollDom.scrollHeight
2.觸底后觸發(fā)列表添加, 列表添加使用createDocumentFragment, 將多次插入的DOM先存入內(nèi)存, 最后一次填充進去, 提高性能, 也方便后面的MutationObserver監(jiān)聽
3.使用MutationObserver監(jiān)聽列表的DOM添加, 添加完畢后, 隱藏加載中提示
示例
https://codepen.io/klren0312/full/dybgayL

參考資料
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/clientHeight
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scrollHeight
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scrollTop
https://developer.mozilla.org/zh-CN/docs/Web/API/GlobalEventHandlers/onscroll
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/createDocumentFragment
https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver
代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.hide {
display: none;
}
.scroll {
height: 200px;
width: 300px;
overflow-y: auto;
border: 1px solid #ddd;
}
.loading {
text-align: center;
}
ul {
margin: 0;
padding: 0;
}
li {
padding: 10px;
margin: 10px;
text-align: center;
background: #FFF6F6;
list-style-type: none;
}
</style>
</head>
<body>
<div id="js-scroll" class="scroll">
<ul id="js-list">
<li>000000</li>
<li>000000</li>
<li>000000</li>
<li>000000</li>
<li>000000</li>
</ul>
<div class="loading hide" id="js-loading">加載中...</div>
</div>
<script>
let index = 0 // 列表個數(shù)
const listDom = document.getElementById('js-list')
const loadingDom = document.getElementById('js-loading')
/**
* 使用MutationObserver監(jiān)聽列表的 DOM 改變
*/
const config = {
attributes: true,
childList: true,
subtree: true
}
const callback = function(mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
if (index === 5) {
loadingDom.innerText = '加載完畢'
} else {
loadingDom.classList.add('hide')
}
}
}
}
const observer = new MutationObserver(callback)
observer.observe(listDom, config)
/**
* clientHeight 滾動可視區(qū)域高度
* scrollTop 當前滾動位置
* scrollHeight 整個滾動高度
*/
const scrollDom = document.getElementById('js-scroll')
scrollDom.onscroll = () => {
if (scrollDom.clientHeight + parseInt(scrollDom.scrollTop) === scrollDom.scrollHeight) {
if (loadingDom.classList.contains('hide') && index <= 5) {
loadingDom.classList.remove('hide')
addList()
}
if (index >= 5) {
observer.disconnect() // 加載完畢停止監(jiān)聽列表 DOM 變化
}
}
}
/**
* 添加列表
*/
function addList () {
const fragment = document.createDocumentFragment()
setTimeout(() => {
++index
for (let i = 0; i < 5; i++) {
const li = document.createElement('li')
li.innerText = new Array(6).fill(index).join('')
fragment.appendChild(li)
}
listDom.appendChild(fragment)
} , 1000)
}
</script>
</body>
</html>
總結(jié)
以上所述是小編給大家介紹的JS實現(xiàn)滾動條觸底加載更多,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
javascript中強制執(zhí)行toString()具體實現(xiàn)
Javascript通常會根據(jù)方法或運算符的需要而自動把值轉(zhuǎn)成所需的類型,這可能導(dǎo)致各種錯誤,接下來為大家介紹下javascript如何強制執(zhí)行toString(),感興趣的朋友可以參考下哈2013-04-04
JavaScript 數(shù)組展平方法: flat() 和 flatMap()詳解
從 ES2019 中開始引入了一種扁平化數(shù)組的新方法,可以展平任何深度的數(shù)組,這篇文章主要介紹了JavaScript 數(shù)組展平方法: flat() 和 flatMap()詳解,需要的朋友可以參考下2023-06-06
在JavaScript中對HTML進行反轉(zhuǎn)義詳解
下面小編就為大家?guī)硪黄贘avaScript中對HTML進行反轉(zhuǎn)義詳解。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
JavaScript中this關(guān)鍵字使用方法詳解
JavaScript中this關(guān)鍵字使用方法詳解...2007-03-03
JavaScript Event學(xué)習(xí)第八章 事件的順序
在第一章中我提到一個初次看起來可能不是那么好理解的是一個問題:“如果一個元素和他的父元素對于同樣的事件都有事件處理程序,那么哪個會首先執(zhí)行呢?”毫無疑問,看是什么瀏覽器。2010-02-02
微信小程序?qū)崿F(xiàn)YDUI的ScrollNav組件
這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)YDUI的ScrollNav組件,滾動導(dǎo)航效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

