基于vue實(shí)現(xiàn)頁面滾動加載的示例詳解
頁面內(nèi)容太多會導(dǎo)致加載速度過慢,這時可考慮使用滾動加載即還沒有出現(xiàn)在可視范圍內(nèi)的內(nèi)容塊先不加載,出現(xiàn)后再加載
基本思路就是判斷元素是否出現(xiàn)在瀏覽器的可視區(qū)域,出現(xiàn)了就加載請求接口加載內(nèi)容。但是要求內(nèi)容塊有最小高度。
具體代碼如下
<template>
<div>
<div
v-loading.fullscreen.lock="loading"
class="page"
id="tablist"
@scroll="listScroll"
>
<div
class="item"
v-for="(item, i) in testData"
:key="i"
:class="i % 2 == 0 ? 'gray' : 'white'"
:id="item.id"
>
{{ item.name }}
</div>
</div>
</div>
</template>
<script>
export default {
name: "ScrollLoadingSample",
props: {},
data() {
return {
flag: true, // 開關(guān)
loading: false, // loading加載
testData: [], // 列表數(shù)據(jù)
targetIndex: 0,
nextId: "",
};
},
mounted() {
this.initData();
},
methods: {
initData() {
this.testData = [
{
id: "test-1",
name: "區(qū)塊1",
// 要加載的方法名
function: "loadTest1",
// 方法是否加載了
isLoaded: false,
},
{
id: "test-2",
name: "區(qū)塊2",
function: "loadTest2",
isLoaded: false,
},
{
id: "test-3",
name: "區(qū)塊3",
function: "loadTest3",
isLoaded: false,
},
{
id: "test-4",
name: "區(qū)塊4",
function: "loadTest4",
isLoaded: false,
},
{
id: "test-5",
name: "區(qū)塊5",
function: "loadTest5",
isLoaded: false,
},
{
id: "test-6",
name: "區(qū)塊6",
function: "loadTest6",
isLoaded: false,
},
];
this.$nextTick(() => {
this.loadPage();
});
},
//判斷元素是否在可視區(qū)域
isElementInViewport(id) {
let el = document.getElementById(id);
//獲取元素是否在可視區(qū)域
let rect = el.getBoundingClientRect();
return (
rect.top + rect.height / 2 <=
(window.innerHeight || document.documentElement.clientHeight) &&
rect.bottom > 0
);
},
// 初次加載頁面時
loadPage() {
for (let i = 0; i < this.testData.length; i++) {
if (this.isElementInViewport(this.testData[i]["id"])) {
this.loadFunctionByName(this.testData[i]["function"]);
this.testData[i]["isLoaded"] = true;
} else {
this.targetIndex = i;
// 最開始沒出現(xiàn)在頁面上的id
this.nextId = this.testData[this.targetIndex]["id"];
break;
}
}
},
// 頁面滑動
listScroll() {
for (let i = this.targetIndex; i < this.testData.length; i++) {
// 如果出現(xiàn)在頁面上
if (this.isElementInViewport(this.testData[i]["id"])) {
// 如果方法沒有加載
if (!this.testData[i]["isLoaded"]) {
this.loadFunctionByName(this.testData[i]["function"]);
this.testData[i]["isLoaded"] = true;
}
} else {
// 如果沒有出現(xiàn)在頁面上
this.targetIndex = i;
this.nextId = this.testData[this.targetIndex]["id"];
break;
}
}
},
// 根據(jù)方法名加載
loadFunctionByName(functionName) {
switch (functionName) {
case "loadTest1":
this.loading = true;
// 模擬延遲請求
setTimeout(() => {
console.log("加載區(qū)塊1");
this.loading = false;
}, 1000);
break;
case "loadTest2":
this.loading = true;
// 模擬延遲請求
setTimeout(() => {
console.log("加載區(qū)塊2");
this.loading = false;
}, 1000);
break;
case "loadTest3":
this.loading = true;
// 模擬延遲請求
setTimeout(() => {
console.log("加載區(qū)塊3");
this.loading = false;
}, 1000);
break;
case "loadTest4":
this.loading = true;
// 模擬延遲請求
setTimeout(() => {
console.log("加載區(qū)塊4");
this.loading = false;
}, 1000);
break;
case "loadTest5":
this.loading = true;
// 模擬延遲請求
setTimeout(() => {
console.log("加載區(qū)塊5");
this.loading = false;
}, 1000);
break;
case "loadTest6":
this.loading = true;
// 模擬延遲請求
setTimeout(() => {
console.log("加載區(qū)塊6");
this.loading = false;
}, 1000);
break;
}
},
},
};
</script>
<style scoped>
.page {
width: 100vw;
height: 100vh;
overflow-y: auto;
display: flex;
flex-wrap: wrap;
/* justify-content: center;
align-items: center; */
}
.item {
width: 100vw;
height: 25vh;
display: flex;
justify-content: center;
align-items: center;
}
.gray {
background-color: #d5d1d1;
}
.white {
background-color: white;
}
.loading {
width: 80px;
height: 100px;
background: rgba(0, 0, 255, 0.664);
display: inline-block;
}
</style>
需要注意 getBoundingClientRect()這個方法,正是通過這個方法獲取元素位置,從而判斷是否出現(xiàn)在可視區(qū)域
到此這篇關(guān)于基于vue實(shí)現(xiàn)頁面滾動加載的示例詳解的文章就介紹到這了,更多相關(guān)vue頁面滾動加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用數(shù)組splice方法失效,且總刪除最后一項的問題及解決
這篇文章主要介紹了vue使用數(shù)組splice方法失效,且總刪除最后一項的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue-cli系列之vue-cli-service整體架構(gòu)淺析
這篇文章主要介紹了vue-cli系列之vue-cli-service整體架構(gòu)淺析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Vue3?vite配置css?的sourceMap及文件引用配置別名的過程
這篇文章主要介紹了Vue3 vite配置css的sourceMap,及文件引用配置別名的過程,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
Vue利用AJAX請求獲取XML文件數(shù)據(jù)的操作方法
在現(xiàn)代Web開發(fā)中,從前端框架到后端API的交互是必不可少的一部分,Vue.js作為一個輕量級且功能強(qiáng)大的前端框架,支持多種方式與服務(wù)器通信,從而獲取或發(fā)送數(shù)據(jù),本文將詳細(xì)介紹如何在Vue.js項目中使用AJAX請求來獲取XML格式的數(shù)據(jù),需要的朋友可以參考下2024-09-09

