基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例詳解
頁(yè)面內(nèi)容太多會(huì)導(dǎo)致加載速度過(guò)慢,這時(shí)可考慮使用滾動(dòng)加載即還沒(méi)有出現(xiàn)在可視范圍內(nèi)的內(nèi)容塊先不加載,出現(xiàn)后再加載
基本思路就是判斷元素是否出現(xiàn)在瀏覽器的可視區(qū)域,出現(xiàn)了就加載請(qǐng)求接口加載內(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, // 開(kāi)關(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 ); }, // 初次加載頁(yè)面時(shí) 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; // 最開(kāi)始沒(méi)出現(xiàn)在頁(yè)面上的id this.nextId = this.testData[this.targetIndex]["id"]; break; } } }, // 頁(yè)面滑動(dòng) listScroll() { for (let i = this.targetIndex; i < this.testData.length; i++) { // 如果出現(xiàn)在頁(yè)面上 if (this.isElementInViewport(this.testData[i]["id"])) { // 如果方法沒(méi)有加載 if (!this.testData[i]["isLoaded"]) { this.loadFunctionByName(this.testData[i]["function"]); this.testData[i]["isLoaded"] = true; } } else { // 如果沒(méi)有出現(xiàn)在頁(yè)面上 this.targetIndex = i; this.nextId = this.testData[this.targetIndex]["id"]; break; } } }, // 根據(jù)方法名加載 loadFunctionByName(functionName) { switch (functionName) { case "loadTest1": this.loading = true; // 模擬延遲請(qǐng)求 setTimeout(() => { console.log("加載區(qū)塊1"); this.loading = false; }, 1000); break; case "loadTest2": this.loading = true; // 模擬延遲請(qǐng)求 setTimeout(() => { console.log("加載區(qū)塊2"); this.loading = false; }, 1000); break; case "loadTest3": this.loading = true; // 模擬延遲請(qǐng)求 setTimeout(() => { console.log("加載區(qū)塊3"); this.loading = false; }, 1000); break; case "loadTest4": this.loading = true; // 模擬延遲請(qǐng)求 setTimeout(() => { console.log("加載區(qū)塊4"); this.loading = false; }, 1000); break; case "loadTest5": this.loading = true; // 模擬延遲請(qǐng)求 setTimeout(() => { console.log("加載區(qū)塊5"); this.loading = false; }, 1000); break; case "loadTest6": this.loading = true; // 模擬延遲請(qǐng)求 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()這個(gè)方法,正是通過(guò)這個(gè)方法獲取元素位置,從而判斷是否出現(xiàn)在可視區(qū)域
到此這篇關(guān)于基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例詳解的文章就介紹到這了,更多相關(guān)vue頁(yè)面滾動(dòng)加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Vue.js基于$.ajax獲取數(shù)據(jù)并與組件的data綁定
這篇文章主要介紹了詳解Vue.js基于$.ajax獲取數(shù)據(jù)并與組件的data綁定,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05Vue.js+HighCharts實(shí)現(xiàn)動(dòng)態(tài)請(qǐng)求展示時(shí)序數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Vue.js+HighCharts實(shí)現(xiàn)動(dòng)態(tài)請(qǐng)求展示時(shí)序數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue監(jiān)聽(tīng)屏幕尺寸變化問(wèn)題,window.onresize很簡(jiǎn)單
這篇文章主要介紹了vue監(jiān)聽(tīng)屏幕尺寸變化問(wèn)題,window.onresize很簡(jiǎn)單,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09詳解基于vue-cli3.0如何構(gòu)建功能完善的前端架子
這篇文章主要介紹了詳解基于vue-cli3.0如何構(gòu)建功能完善的前端架子,本文整合出具備基礎(chǔ)功能的前端架子,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10使用Vue快速實(shí)現(xiàn)一個(gè)無(wú)縫輪播效果
這篇文章主要為大家詳細(xì)介紹了如何使用Vue快速實(shí)現(xiàn)一個(gè)無(wú)縫輪播效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-04-04vue項(xiàng)目打包后放服務(wù)器非根目錄下圖片找不到問(wèn)題
這篇文章主要介紹了vue項(xiàng)目打包后放服務(wù)器非根目錄下圖片找不到問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12