使用vue實(shí)現(xiàn)滑動(dòng)滾動(dòng)條來(lái)加載數(shù)據(jù)
實(shí)現(xiàn)思路
- 首先,我們需要在vuejs中引入
axios - 然后,我們需要從
vue中,引入onMounted,onUnmounted生命周期鉤子函數(shù) 然后,我們需要在onMounted函數(shù)中,進(jìn)行監(jiān)聽(tīng) 而在onUnmounted函數(shù)中,我們需要取消監(jiān)聽(tīng),解綁 - 編寫(xiě)事件處理函數(shù)
handleScroll, 獲取變量scrollTop是滾動(dòng)條滾動(dòng)時(shí),距離頂部的距離,獲取變量scrollHeight是滾動(dòng)條的總高度,獲取變量clientHeight是滾動(dòng)條可視區(qū)域的高度 - 當(dāng)滾動(dòng)條到達(dá)底部,并且距離底部小于10px時(shí),加載數(shù)據(jù),也就是請(qǐng)求axios數(shù)據(jù),頁(yè)碼++,重新加載數(shù)據(jù)函數(shù)
- 為了防止用戶頻繁觸發(fā)下拉滑動(dòng)滾動(dòng)條,往往需要添加一個(gè)函數(shù)防抖,在指定的時(shí)間內(nèi),只執(zhí)行最后一次事件處理函數(shù),避免頻繁請(qǐng)求數(shù)據(jù),給服務(wù)器造成壓力
代碼實(shí)現(xiàn)
<template>
<div>
<div>
<el-button type="primary" @click="handleBtnGetJoke">請(qǐng)求數(shù)據(jù)</el-button>
<el-button type="danger" @click="handleBtnClearData" v-if="aDatas.length > 0?true:false">清空數(shù)據(jù)</el-button>
</div>
<div>
<ul v-if="aDatas.length > 0?true:false">
<li class="joke-list" v-for="item in aDatas"
:key="item.hashId">{{ item.content }}
</li>
<div class="loading" v-if="aDatas.length > 0?true:false">
<el-button size="mini" type="primary" @click="handleBtnLoading" >加載</el-button>
</div>
</ul>
</div>
</div>
</template>
<script setup>
import axios from "axios";
import { ref,onMounted,onUnmounted } from "vue";
let aDatas = ref([]);
let page = ref(1);
let pagesize = ref(20);
onMounted(() => {
// 獲取數(shù)據(jù)
handleBtnGetJoke();
window.addEventListener('scroll', debounce(handleScroll,500));
})
onUnmounted(() => {
window.removeEventListener('scroll', handleScroll);
})
// 事件處理函數(shù)
function handleScroll() {
// 變量scrollTop是滾動(dòng)條滾動(dòng)時(shí),距離頂部的距離
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
// 變量scrollHeight是滾動(dòng)條的總高度
const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
// 變量clientHeight是滾動(dòng)條可視區(qū)域的高度
const clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
// 當(dāng)滾動(dòng)條到達(dá)底部,并且距離底部小于10px時(shí),加載數(shù)據(jù)
if (scrollTop + clientHeight - scrollHeight <= 10) {
page.value++;
handleBtnGetJoke();
}
}
// 函數(shù)的防抖
function debounce(method, duration) {
var timer = null;
return function(){
var that = this,
args = arguments;
// 在本次調(diào)用之間的一個(gè)間隔時(shí)間內(nèi)若有方法在執(zhí)行,則終止該方法的執(zhí)行
if(timer) {
clearTimeout(timer);
}
// 開(kāi)始執(zhí)行本次調(diào)用
timer = setTimeout(function(){
method.apply(that,args);
}, duration)
}
}
async function handleBtnGetJoke() {
const params = {
key: 'aa1b7ad08be2a09a4e0d2d46897e2dc8',
page: page.value,
pagesize:pagesize.value,
time: 1418816972
}
const response = await axios.get('/api/joke/content/text.php',{params})
console.log(response);
if(response.status == 200) {
const { data } = response.data.result;
console.log(data);
aDatas.value = aDatas.value.concat(data);
if(page.value*pagesize.value >= data.length) {
alert("沒(méi)有更多數(shù)據(jù)了")
}
}
}
// 加載數(shù)據(jù),疊加
function handleBtnLoading() {
page.value++;
handleBtnGetJoke();
}
// 清空數(shù)據(jù)
function handleBtnClearData() {
aDatas.value = [];
}
</script>
<style scoped>
.joke-list {
list-style-type: decimal;
list-style-position: outside;
padding: 5px 0;
border-bottom: dashed 1px #ccc;
}
.loading {
margin: 0 auto;
text-align:center;
}
</style>
其中核心防抖函數(shù)如下所示,實(shí)現(xiàn)方式也很簡(jiǎn)單,就是利用定時(shí)器,在規(guī)定的時(shí)間內(nèi),如果再次觸發(fā),則清除定時(shí)器,重新開(kāi)始計(jì)時(shí)。
只執(zhí)行最后一次
// 函數(shù)的防抖
function debounce(method, duration) {
var timer = null;
return function(){
var that = this,
args = arguments;
// 在本次調(diào)用之間的一個(gè)間隔時(shí)間內(nèi)若有方法在執(zhí)行,則終止該方法的執(zhí)行
if(timer) {
clearTimeout(timer);
}
// 開(kāi)始執(zhí)行本次調(diào)用
timer = setTimeout(function(){
method.apply(that,args);
}, duration)
}
}
至于怎么樣判斷數(shù)據(jù)是否加載完畢,到最后一頁(yè)
每次在請(qǐng)求完成數(shù)據(jù)的時(shí)候去判斷一下當(dāng)前的 page × pagesize是否已經(jīng)大于等于接口返回的total值就行了,也可以是pageNum 等于total 的時(shí)候,就說(shuō)明已經(jīng)沒(méi)有數(shù)據(jù)了,可以提示用戶了
if(page.value*pagesize.value >= data.length) {
alert("沒(méi)有更多數(shù)據(jù)了")
}
總結(jié)
其實(shí)這個(gè)功能很簡(jiǎn)單,但是寫(xiě)起來(lái)還是有點(diǎn)麻煩,因?yàn)樯婕暗疆惒秸?qǐng)求,所以需要判斷數(shù)據(jù)是否加載完畢,還要判斷是否最后一頁(yè),還要判斷是否還有數(shù)據(jù),還要判斷是否需要提示用戶沒(méi)有更多數(shù)據(jù)了,所以代碼量還是挺多的,但是寫(xiě)完之后,感覺(jué)還是挺有成就感的。
什么上拉,下拉刷新,下拉加載更多,其實(shí)原理都差不多,都是利用了防抖函數(shù),然后利用定時(shí)器,在規(guī)定的時(shí)間內(nèi),如果再次觸發(fā),則清除定時(shí)器,重新開(kāi)始計(jì)時(shí)。 實(shí)現(xiàn)方式都差不多
以上就是vuejs實(shí)現(xiàn)滑動(dòng)滾動(dòng)條來(lái)加載數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于vuejs滾動(dòng)條加載數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue + element-ui實(shí)現(xiàn)簡(jiǎn)潔的導(dǎo)入導(dǎo)出功能
Element-UI是餓了么前端團(tuán)隊(duì)推出的一款基于Vue.js 2.0 的桌面端UI框架,手機(jī)端有對(duì)應(yīng)框架是 Mint UI,下面這篇文章主要給大家介紹了關(guān)于利用vue + element-ui如何實(shí)現(xiàn)簡(jiǎn)潔的導(dǎo)入導(dǎo)出功能的相關(guān)資料,需要的朋友可以參考下。2017-12-12
el-select 數(shù)據(jù)回顯,只顯示value不顯示lable問(wèn)題
這篇文章主要介紹了el-select 數(shù)據(jù)回顯,只顯示value不顯示lable問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue.js與 ASP.NET Core 服務(wù)端渲染功能整合
本文通過(guò)案例給大家詳細(xì)分析了ASP.NET Core 與 Vue.js 服務(wù)端渲染,需要的朋友可以參考下2017-11-11
vue實(shí)現(xiàn)html轉(zhuǎn)word與word全過(guò)程
本文介紹了如何在Vue中實(shí)現(xiàn)HTML轉(zhuǎn)Word功能,通過(guò)拖拽組件生成類似Word的界面,并實(shí)時(shí)預(yù)覽,主要使用了htmlDocx和htmlToDocx插件將HTML轉(zhuǎn)換為.docx格式,同時(shí),還討論了檢查.docx文件是否損壞的方法2026-01-01
vue的狀態(tài)庫(kù)管理實(shí)現(xiàn)示例
Vuex 是 Vue.js 官方推薦的狀態(tài)管理庫(kù)之一,本文主要介紹了vue的狀態(tài)庫(kù)管理實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Vue2.x中的父組件傳遞數(shù)據(jù)至子組件的方法
這篇文章主要介紹了Vue2.x中的父組件數(shù)據(jù)傳遞至子組件的方法,需要的朋友可以參考下2017-05-05
VUE3中Element table表頭動(dòng)態(tài)展示合計(jì)信息
本文主要介紹了在Vue中實(shí)現(xiàn)動(dòng)態(tài)合計(jì)兩個(gè)字段并輸出摘要信息的方法,通過(guò)使用監(jiān)聽(tīng)器和深度監(jiān)聽(tīng),確保當(dāng)數(shù)據(jù)變化時(shí)能正確更新合計(jì)結(jié)果,具有一定的參考價(jià)值,感興趣的可以了解一下2024-11-11

