基于vue實現頁面滾動加載的示例詳解
頁面內容太多會導致加載速度過慢,這時可考慮使用滾動加載即還沒有出現在可視范圍內的內容塊先不加載,出現后再加載
基本思路就是判斷元素是否出現在瀏覽器的可視區(qū)域,出現了就加載請求接口加載內容。但是要求內容塊有最小高度。
具體代碼如下
<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, // 開關
loading: false, // loading加載
testData: [], // 列表數據
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;
// 最開始沒出現在頁面上的id
this.nextId = this.testData[this.targetIndex]["id"];
break;
}
}
},
// 頁面滑動
listScroll() {
for (let i = this.targetIndex; i < this.testData.length; i++) {
// 如果出現在頁面上
if (this.isElementInViewport(this.testData[i]["id"])) {
// 如果方法沒有加載
if (!this.testData[i]["isLoaded"]) {
this.loadFunctionByName(this.testData[i]["function"]);
this.testData[i]["isLoaded"] = true;
}
} else {
// 如果沒有出現在頁面上
this.targetIndex = i;
this.nextId = this.testData[this.targetIndex]["id"];
break;
}
}
},
// 根據方法名加載
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()這個方法,正是通過這個方法獲取元素位置,從而判斷是否出現在可視區(qū)域
到此這篇關于基于vue實現頁面滾動加載的示例詳解的文章就介紹到這了,更多相關vue頁面滾動加載內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Vue.js基于$.ajax獲取數據并與組件的data綁定
這篇文章主要介紹了詳解Vue.js基于$.ajax獲取數據并與組件的data綁定,非常具有實用價值,需要的朋友可以參考下2017-05-05
Vue.js+HighCharts實現動態(tài)請求展示時序數據
這篇文章主要為大家詳細介紹了Vue.js+HighCharts實現動態(tài)請求展示時序數據,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
vue監(jiān)聽屏幕尺寸變化問題,window.onresize很簡單
這篇文章主要介紹了vue監(jiān)聽屏幕尺寸變化問題,window.onresize很簡單,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
解決vue與node模版引擎的渲染標記{{}}(雙花括號)沖突問題
這篇文章主要介紹了解決vue與node模版引擎的渲染標記{{}}(雙花括號)沖突問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

