JS中頁(yè)面列表加載的常用方法總結(jié)
導(dǎo)語(yǔ):最近由于一些事情需要處理,所以沒(méi)來(lái)得及寫技術(shù)總結(jié)了。今天終于可以坐下來(lái)好好的梳理一下脈絡(luò),說(shuō)一下那個(gè)在日常前端開(kāi)發(fā)過(guò)程中,常用到的頁(yè)面列表加載的方法總結(jié)。這里介紹三種方法,分別是分頁(yè)加載、按鈕加載以及滾動(dòng)加載。
方法簡(jiǎn)介
在日常的前端開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)碰到列表很長(zhǎng),不可能完全顯示出來(lái),所以就要進(jìn)行分頁(yè),每頁(yè)固定顯示幾條,然后下面是頁(yè)數(shù),點(diǎn)到哪頁(yè)顯示哪頁(yè)的內(nèi)容。
除了常見(jiàn)的分頁(yè)加載外,還要點(diǎn)擊按鈕加載,這種加載方法就是不需要點(diǎn)擊下一頁(yè)這種了,直接點(diǎn)擊按鈕往第一頁(yè)的后面補(bǔ)上下一頁(yè)的內(nèi)容,非常方便。
除了以上兩種,滾動(dòng)加載也是用的比較多的一種列表加載方法,下面就這三種方法做一下總結(jié)歸納,方便需要的小伙伴們使用。
封裝實(shí)現(xiàn)
下面就對(duì)三種方法分別做一下原理解析和方法實(shí)現(xiàn)。
下面的列表使用了JSONPlaceholder站點(diǎn)上的一些數(shù)據(jù)作為列表來(lái)源。
分頁(yè)加載
當(dāng)頁(yè)面的需求是要顯示一個(gè)列表或者表格,總數(shù)很多放不下,這時(shí)候可以把全部的數(shù)據(jù)分成多頁(yè),每頁(yè)顯示固定的條數(shù),計(jì)算出總頁(yè)數(shù),然后渲染一下就可以了。
頁(yè)面布局
<div class="wrap">
<ul id="list"></ul>
<ul id="pages"></ul>
</div>
.wrap {
max-width: 960px;
margin: 0 auto;
padding: 15px 0;
}
.wrap li {
padding: 5px 0;
list-style: square;
}
.wrap li h3,
.wrap li p {
text-transform: capitalize;
}
.wrap li h3:hover {
color: #f00;
cursor: pointer;
}
#pages li {
display: inline-block;
margin-right: 10px;
list-style: none;
}
#pages button {
width: auto;
min-width: 40px;
height: 40px;
background: #fff;
box-shadow: 0 0 5px #fff;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
#pages button:hover,
#pages button.active {
color: #fff;
border-color: #f00;
background: #f00;
cursor: pointer;
}
#pages button.dis {
cursor: no-drop;
}
.wrap .loading {
line-height: 70vh;
text-align: center;
list-style: none;
}
.wrap .nodata {
list-style: none;
text-align: center;
}
定義變量
let datas = [], // 分組列表
current = 1, // 當(dāng)前頁(yè)
pages = 0, // 總頁(yè)數(shù)
total = 0, // 總數(shù)
listElem = document.getElementById('list'), // 列表內(nèi)容
pageElem = document.getElementById('pages'); // 頁(yè)數(shù)按鈕
處理數(shù)據(jù)
我們使用axios來(lái)獲取json數(shù)據(jù),模擬抓取接口的情況。
// 獲取列表
async function getList (page = 1) {
let res = await axios.get('https://jsonplaceholder.typicode.com/posts');
if (res.status === 200) {
let data = sliceData(res.data);
pages = data.pages;
total = res.data.length;
datas = [...data.list];
return {
code: 200,
msg: 'get_succ',
data: {
list: data.list[page-1],
current: page,
pages: data.pages,
total: list.length,
}
}
}
}
寫一個(gè)切割數(shù)組的方法,分成等份的數(shù)組。
// 處理數(shù)據(jù)
function sliceData (list) {
let newArr = [],step = 10,pages = Math.ceil(list.length/10);
for (let i = 0; i < list.length; i+=step) {
let item = list.slice(i, i+step);
newArr.push(item);
}
return {
list: newArr,
pages,
};
}
顯示列表
showList(current);
// 顯示列表
async function showList (current) {
let data = null;
listElem.innerHTML = '';
listElem.innerHTML = '<li class="loading">加載中...</li>';
if (datas && datas.length) {
data = {
code: 200,
msg: 'get_succ',
data: {
list: datas[current-1],
current: current,
pages,
total,
}
}
} else {
data = await getList(current);
}
if (data.code === 200) {
let list = data.data.list;
if (list && list.length) {
let liStr = '',pageStr = '';
for (const item of list) {
liStr += `<li>
<h3>${item.title}</h3>
<p>${item.body}</p>
</li>`;
}
setTimeout(() => {
listElem.innerHTML = liStr;
}, 1000);
if (pageElem.innerText === '') {
for (let i = 0; i < data.data.pages; i++) {
pageStr += `<li><button class="page" data-id="${i+1}">${i+1}</button></li>`
}
pageElem.innerHTML = `
<li><button id="start" data-id="1">首頁(yè)</button></li>
<li><button id="prev">上一頁(yè)</button></li>
${pageStr}
<li><button id="next">下一頁(yè)</button></li>
<li><button id="end" data-id="${data.data.pages}">尾頁(yè)</button></li>`;
showHighLight(current);
addClick();
}
} else {
listElem.innerHTML = '<li class="nodata">暫無(wú)數(shù)據(jù)</li>';
}
}
}
添加點(diǎn)擊事件
// 添加點(diǎn)擊
function addClick () {
let btns = document.querySelectorAll('#pages li button');
for (const item of btns) {
item.addEventListener('click', toggleList, false);
}
}
切換頁(yè)面內(nèi)容
// 切換頁(yè)面
function toggleList (event) {
let id = event.target.dataset.id,
bid = event.target.id;
if (id) {
current = Number(id);
}
if (bid == 'prev') {
if (current <= 1) {
current = 1;
} else {
current--;
}
} else if (bid == 'next') {
if (current >= pages) {
current = pages;
} else {
current++;
}
}
showHighLight(current, bid);
showList(current);
}
顯示高亮
// 顯示高亮
function showHighLight (current, bid) {
let btns = document.querySelectorAll('.page'),
startBtn = document.getElementById('start'),
endBtn = document.getElementById('end');
for (const item of btns) {
item.className = 'page';
}
btns[current-1].className = 'page active';
startBtn.className = current == 1 ? 'active dis' : '';
endBtn.className = current == pages ? 'active dis' : '';
}
其中渲染好頁(yè)面后,還加了一個(gè)定時(shí)器是模擬從服務(wù)器獲取數(shù)據(jù)等待過(guò)程的效果,真實(shí)情況下不需要這樣。
按鈕加載
按鈕加載的方法和上面的相似,也就是分頁(yè)那塊改成一個(gè)按鈕了,不斷在現(xiàn)有的列表中添加新的列表內(nèi)容,其余和分頁(yè)加載沒(méi)有太大區(qū)別。
頁(yè)面結(jié)構(gòu)
<div class="wrap">
<ul id="list"></ul>
<p class="loadmore">加載中...</p>
<p class="more-box">
<button id="more">加載更多</button>
</p>
</div>
頁(yè)面美化
.more-box {
text-align: center;
}
#more {
padding: 10px;
background: none;
border: 1px solid #ccc;
border-radius: 5px;
}
#more:hover {
cursor: pointer;
border-color: #f00;
background-color: #f00;
color: #fff;
}
.loadmore {
text-align: center;
}
.hide {
display: none;
}
獲取變量
let loadMore = document.querySelector('.loadmore'),
moreBtn = document.getElementById('more');
點(diǎn)擊加載更多
// 添加點(diǎn)擊
moreBtn.addEventListener('click', addList, false);
// 切換頁(yè)面
function addList () {
if (current < pages) {
current+=1;
showList(current);
} else {
moreBtn.innerText = '沒(méi)有更多了';
}
}
顯示頁(yè)面
在原有的顯示列表方法基礎(chǔ)上修改幾處就好了。
// 顯示列表
async function showList (current) {
let data = null;
loadMore.className = 'loadmore';
if (datas && datas.length) {
data = {
code: 200,
msg: 'get_succ',
data: {
list: datas[current-1],
current: current,
pages,
total,
}
}
} else {
data = await getList(current);
}
if (data.code === 200) {
let list = data.data.list;
if (list && list.length) {
let liStr = '',pageStr = '';
for (const item of list) {
liStr += `<li>
<h3>${item.title}</h3>
<p>${item.body}</p>
</li>`;
}
listElem.innerHTML += liStr;
} else {
listElem.innerHTML = '<li class="nodata">暫無(wú)數(shù)據(jù)<li>';
}
setTimeout(() => {
loadMore.className = 'loadmore hide';
}, 1000);
}
}
滾動(dòng)加載
滾動(dòng)加載就是在頁(yè)面滾動(dòng)到底部后自動(dòng)添加新的一頁(yè)內(nèi)容到當(dāng)前列表后面,每次滾動(dòng)根據(jù)計(jì)算動(dòng)態(tài)添加內(nèi)容。
就是在按鈕加載的基礎(chǔ)上更改而來(lái)的,具體的原理是當(dāng)文檔的到頂部的高度加上文檔的可視化高度大于文檔的滾動(dòng)高度的時(shí)候就加載頁(yè)面。
頁(yè)面結(jié)構(gòu)
<div class="wrap">
<ul id="list"></ul>
<p class="loadmore">加載中...</p>
</div>
滾動(dòng)判斷
document.addEventListener('scroll', checkScroll, false);
function checkScroll () {
let scrollTop = document.documentElement.scrollTop,
clientHei = document.documentElement.clientHeight,
scrollHeight = document.documentElement.scrollHeight;
if (scrollTop + clientHei >= scrollHeight) {
addList();
}
}
// 切換頁(yè)面
function addList () {
if (current < pages) {
current+=1;
showList(current);
} else {
loadMore.innerText = '沒(méi)有更多了';
}
}
效果預(yù)覽
分頁(yè)加載

按鈕加載

滾動(dòng)加載

到此這篇關(guān)于JS中頁(yè)面列表加載的常用方法總結(jié)的文章就介紹到這了,更多相關(guān)JS頁(yè)面列表加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js中小數(shù)向上取整數(shù),向下取整數(shù),四舍五入取整數(shù)的實(shí)現(xiàn)(必看篇)
下面小編就為大家?guī)?lái)一篇js中小數(shù)向上取整數(shù),向下取整數(shù),四舍五入取整數(shù)的實(shí)現(xiàn)(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
uniapp中實(shí)現(xiàn)canvas超出屏幕滾動(dòng)查看功能
親愛(ài)的小伙伴,當(dāng)你需要在uniapp中使用canvas繪制一個(gè)超長(zhǎng)圖,就類似于橫向的流程圖時(shí),這個(gè)canvas超出屏幕部分拖動(dòng)屏幕查看會(huì)變得十分棘手,怎么解決這個(gè)問(wèn)題呢,下面小編給大家介紹uniapp中實(shí)現(xiàn)canvas超出屏幕滾動(dòng)查看功能,感興趣的朋友一起看看吧2024-03-03
javascript replace方法與正則表達(dá)式
replace方法的語(yǔ)法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正則表達(dá)式對(duì)象(RegExp)也可以是字符串(string),replaceText是替代查找到的字符串。。為了幫助大家更好的理解,下面舉個(gè)簡(jiǎn)單例子說(shuō)明一下2008-02-02

