使用vue-infinite-scroll實現(xiàn)無限滾動效果
vue-infinite-scroll插件可以無限滾動實現(xiàn)加載更多,其作用是是當滾動條滾動到距離底部的指定高度時觸發(fā)某個方法。
https://github.com/ElemeFE/vue-infinite-scroll/
https://www.npmjs.com/package/vue-infinite-scroll
npm i vue-infinite-scroll --save
main.js使用
import vueiInfinite from 'vue-infinite-scroll' Vue.use(vueiInfinite) <div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10"> <div class="loading">加載中...</div> </div>
1.loadMore是方法,里面是要執(zhí)行的代碼
2.busy的值是true的時候,就不再加載,如果是false就執(zhí)行加載
3.10表示距離底部為10 的時候就執(zhí)行l(wèi)oadMore方法
loadMore () { this.busy = true //把busy置位true,這次請求結(jié)束前不再執(zhí)行 setTimeout(() => { this.page++ this.getGoodLists(true) //調(diào)用獲取數(shù)據(jù)接口,并且傳入一個true,讓axios方法指導是否需要拼接數(shù)組。 }, 500) } getGoodLists (flag) { var param = { page: this.page, pageSize: this.pageSize, sort: this.sortFlag ? 1 : -1 } axios.get('/goods', {params: param}).then((response) => { let res = response.data if (flag) { this.goodList = this.goodList.concat(res.result.list) //如果是flagtrue,則拼接數(shù)組。 if (res.result.count === 0) { this.busy = true } else { this.busy = false } } else { this.goodList = res.result.list this.busy = false 第一次進來的時候,把busy置位false。執(zhí)行l(wèi)oadMore的方法 } }) },
總結(jié)
以上所述是小編給大家介紹的使用vue-infinite-scroll實現(xiàn)無限滾動效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue3 去除 vue warn 及生產(chǎn)環(huán)境去除console.log的方法
這篇文章主要介紹了Vue3 去除 vue warn 及生產(chǎn)環(huán)境去除console.log的方法,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06