vue移動(dòng)UI框架滑動(dòng)加載數(shù)據(jù)的方法
前言
在我們移動(dòng)端還有一個(gè)很常用的組件,那就是滑動(dòng)加載更多組件。平常我們看到的很多插件實(shí)現(xiàn)相當(dāng)復(fù)雜就覺(jué)得這個(gè)組件很難,其實(shí)不是的!!這個(gè)組件其實(shí)可以很簡(jiǎn)單的就實(shí)現(xiàn)出來(lái),而且體驗(yàn)也能非常的棒(當(dāng)然我們沒(méi)有實(shí)現(xiàn)下拉刷新功能)??!下面我們就一起來(lái)實(shí)現(xiàn)這個(gè)組件。
效果展示
先上一個(gè)gif圖片展示我們做成后的效果,如下:
DOM結(jié)構(gòu)
頁(yè)面應(yīng)該包含三個(gè)部分:1. 正文區(qū)域 2.加載小菊花以及記載文字 3.所有數(shù)據(jù)加載完成后的文字:
<div ref="scroll" class="r-scroll"> <div class="r-scroll-wrap"> <slot></slot> </div> <slot name="loading"> <div v-show="isLoading" class="r-scroll-loading"> <r-loading></r-loading> <span class="r-scroll-loading-text">{{loadingText}}</span> </div> </slot> <slot name="complate"> <div v-show="isComplate" class="r-scroll-loading">{{complateText}}</div> </slot> </div>
css樣式
整個(gè)組件的容器r-scroll應(yīng)該是固定寬度,超出部分可以滾動(dòng)的;正文區(qū)域應(yīng)該是隨著內(nèi)容,高度自動(dòng)增長(zhǎng)的;加載小菊花在滾動(dòng)距離底部默認(rèn)數(shù)值的時(shí)候顯示;所有數(shù)據(jù)加載完成后顯示數(shù)據(jù)加載完成文字:
<style lang="scss"> @mixin one-screen { position: absolute; left:0; top:0; width:100%; height:100%; overflow: hidden; } @mixin overflow-scroll { overflow: scroll; -webkit-overflow-scrolling: touch; } .r-scroll{ @include one-screen; @include overflow-scroll; &-loading{ text-align: center; padding-top: 3vw; padding-bottom: 3vw; font-size: 14px; color: #656565; line-height: 20px; &-text{ display: inline-block; vertical-align: middle; } } } </style>
javascript
交互邏輯分析:
- 頁(yè)面初始化的時(shí)候,獲取整個(gè)組件節(jié)點(diǎn)以及正文容器節(jié)點(diǎn)
- 對(duì)整個(gè)容器節(jié)點(diǎn)進(jìn)行綁定scroll事件
- 容器進(jìn)行滾動(dòng)的過(guò)程中判斷是否距離頂部小于指定數(shù)值,如果小于則觸發(fā)自定義事件loadmore
- 業(yè)務(wù)代碼中監(jiān)聽(tīng)loadmore事件,如果觸發(fā)則加載數(shù)據(jù)
因?yàn)榇a不復(fù)雜,故不詳細(xì)解析,大家看下代碼注釋,如有不清楚的請(qǐng)?jiān)谠u(píng)論中發(fā)表評(píng)論:
<script> import rLoading from '../loading' export default{ components: {rLoading}, props: { // 距離底部數(shù)值,小于或等于該數(shù)值觸發(fā)自定義事件loadmore bottomDistance: { type: [Number, String], default: 70 }, // 加載中的文字 loadingText: { type: String, default: '加載中...' }, // 數(shù)據(jù)加載完成的文字 complateText: { type: String, default: '-- 我是個(gè)有底線的列表 --' } }, data () { return { // 用來(lái)判定數(shù)據(jù)是否加載完成 isComplate: false, // 用來(lái)判定是否正在加載數(shù)據(jù) isLoading: false, // 組件容器 scroll: null, // 正文容器 scrollWrap: null } }, watch: { // 監(jiān)聽(tīng)isLoading,如果isLoading的值為true則代表觸發(fā)了loadmore事件 isLoading (val) { if (val) { this.$emit('loadmore') } } }, methods: { // 初始化組件,獲取組件容器、正文容器節(jié)點(diǎn),并給組件容器節(jié)點(diǎn)綁定滾動(dòng)事件 init () { this.scroll = this.$refs.scroll this.scrollWrap = this.scroll.childNodes[0] this.scroll.addEventListener('scroll', this.scrollEvent) this.$emit('init', this.scroll) }, scrollEvent (e) { // 如果數(shù)據(jù)全部加載完成了,則再也不觸發(fā)loadmore事件 if (this.isComplate) return let scrollTop = this.scroll.scrollTop let scrollH = this.scroll.offsetHeight let scrollWrapH = this.scrollWrap.offsetHeight // 組件容器滾的距離 + 組件容器本身距離大于或者等于正文容器高度 - 指定數(shù)值 則觸發(fā)loadmore事件 if (scrollTop + scrollH >= scrollWrapH - this.bottomDistance) { this.isLoading = true } }, // 當(dāng)前數(shù)據(jù)加載完成后調(diào)用該函數(shù) loaded () { this.isLoading = false }, // 所有數(shù)據(jù)加載完成后調(diào)用該函數(shù) compleate () { this.isLoading = false this.isComplate = true this.scroll.removeEventListener('scroll', this.scrollEvent) } }, mounted () { this.$nextTick(this.init) } } </script>
另外該組件中引用到了loading小菊花組件,附錄一個(gè)小菊花組件代碼,因代碼簡(jiǎn)單故不詳細(xì)解析:
菊花使用的是一張gif圖片,請(qǐng)照一張你喜歡的菊花gif放在該菊花組件的路徑下
<template> <div class="r-loading-container"> <img src="./loading.gif"> </div> </template> <script> export default {} </script> <style lang="scss"> .r-loading-container{ display: inline-block; vertical-align: middle; img{ width: 20px; height: 20px; display: block; } } </style>
寫(xiě)在最后
最后這里附錄一個(gè)使用例子吧:
<template> <div class="index"> <r-scroll ref="scroll" @loadmore="queryDate"> <div class="item" v-for="(item, index) in list">{{item}}</div> </r-scroll> </div> </template> <script> import rScroll from '../../components/scroll' function timeout (ms) { return new Promise((resolve, reject) => { setTimeout(resolve, ms, 'done') }) } export default{ components: {rScroll}, data () { return { i: 0, list: [] } }, methods: { async queryDate () { await timeout(1000) let i = this.i let data = [] for (let j = 0; j < 40; j++) { data.push(i + j) this.i = this.i + 1 } this.list = this.list.concat(data) // 調(diào)用組件中的loaded函數(shù),如果數(shù)據(jù)加載完成后記得調(diào)用組件的compleate函數(shù) this.$refs.scroll.loaded() } }, mounted () { this.queryDate() } } </script> <style lang="scss"> .item{ background-color: #f2f2f2; border-bottom: 1px solid #fff; height: 40px; line-height: 40px; text-align: center; } </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用vue實(shí)現(xiàn)滑動(dòng)滾動(dòng)條來(lái)加載數(shù)據(jù)
- vue2中的el-select組件數(shù)據(jù)太多,如何實(shí)現(xiàn)滾動(dòng)加載數(shù)據(jù)效果
- vue中el-table實(shí)現(xiàn)無(wú)限向下滾動(dòng)懶加載數(shù)據(jù)
- Vue實(shí)現(xiàn)網(wǎng)頁(yè)首屏加載動(dòng)畫(huà)及頁(yè)面內(nèi)請(qǐng)求數(shù)據(jù)加載loading效果
- vue實(shí)現(xiàn)列表滑動(dòng)下拉加載數(shù)據(jù)的方法
相關(guān)文章
vue-cli3+typescript初體驗(yàn)小結(jié)
這篇文章主要介紹了vue-cli3+typescript初體驗(yàn)小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02vue & vue Cli 版本及對(duì)應(yīng)關(guān)系解讀
這篇文章主要介紹了vue & vue Cli 版本及對(duì)應(yīng)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說(shuō)明
這篇文章主要介紹了關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04使用 Vue 實(shí)現(xiàn)一個(gè)虛擬列表的方法
這篇文章主要介紹了使用 Vue 實(shí)現(xiàn)一個(gè)虛擬列表的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Vue2中使用axios的3種方法實(shí)例總結(jié)
axios從功能上來(lái)說(shuō)就是主要用于我們前端向后端發(fā)送請(qǐng)求,是基于http客戶端的promise,面向?yàn)g覽器和nodejs,下面這篇文章主要給大家介紹了關(guān)于Vue2中使用axios的3種方法,需要的朋友可以參考下2022-09-09vue 父組件通過(guò)$refs獲取子組件的值和方法詳解
今天小編就為大家分享一篇vue 父組件通過(guò)$refs獲取子組件的值和方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11使用Webpack提高Vue.js應(yīng)用的方式匯總(四種)
Webpack是開(kāi)發(fā)Vue.js單頁(yè)應(yīng)用程序的重要工具。下面通過(guò)四種方式給大家介紹使用Webpack提高Vue.js應(yīng)用,需要的的朋友參考下吧2017-07-07