您好,登錄后才能下訂單哦!
vue移動端實現下拉刷新和上滑加載?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
組件
<template> <div class="mu-load-more" @touchstart="touchStart($event)" @touchmove="touchMove($event)" @touchend="touchEnd($event)"> <div class="mu-refresh-control" v-if="!isNaN(top) && top !== 0" :> <svg-icon icon-class="gengxin" class="mu-refresh-svg-icon" v-if="state === 0 || state === 1" :></svg-icon> </div> <div class="mu-refresh-control son" v-if="state === 2" :> <svg-icon icon-class="jianchagengxin" class="mu-refresh-svg-icon refresh" v-if="state === 2"></svg-icon> </div> <slot></slot> </div> </template> <script> export default { props: { offset: { type: Number, default: 40 }, enableInfinite: { type: Boolean, default: true }, enableRefresh: { type: Boolean, default: true }, onRefresh: { type: Function, default: undefined, required: false }, onInfinite: { type: Function, default: undefined, require: false } }, data() { return { top: 0, state: 0, // 開始滑動時,y軸位置 startY: 0, startScroll: 0, touching: false, infiniteLoading: false, refreshShow: true, infiniteState: true, showLoad: false, marginTop: 0 } }, created(){ if(this.enableRefresh === false) { this.refreshShow = false } window.addEventListener('scroll', this.onScroll) }, destroyed () { window.removeEventListener('scroll', this.onScroll) }, methods: { // 觸摸開始(手指放在觸摸屏上) touchStart(e) { if(window.pageYOffset > 0) return if(!this.enableRefresh) return this.startY = e.targetTouches[0].pageY this.startScroll = this.$el.scrollTop || 0 //開啟滑動記錄 this.touching = true }, // 拖動(手指在觸摸屏上移動) touchMove(e) { // 這里控制是否可以上下拉 代表正在滑動 if (!this.enableRefresh || this.$el.scrollTop > 0 || !this.touching) { return } // 獲取拉取的間隔差 當前移動的y點 初始的y點 初始頂部距離 let diff = e.targetTouches[0].pageY - this.startY - this.startScroll //如果是往上滑的話,就取消事件 if (diff > 0) e.preventDefault() // 對狀態進行處理,看是否處于刷新中 this.top = Math.pow(diff, 0.8) + (this.state === 2 ? this.offset : 0) if (this.state === 2) { // in refreshing return } if (this.top >= this.offset) { this.state = 1 } else { this.state = 0 } }, // 觸摸結束(手指從觸摸屏上移開) touchEnd() { if (!this.enableRefresh) return this.touching = false if (this.state === 2) { this.state = 2 this.top = 0 return } if (this.top >= this.offset) { this.refresh() } else { this.state = 0 this.top = 0 } }, refresh() { this.marginTop = this.top this.state = 2 this.top = 0 this.onRefresh(this.refreshDone) }, refreshDone() { this.state = 0 this.top = 0 this.marginTop = 0 }, infinite() { this.infiniteLoading = true this.onInfinite(this.infiniteDone) }, infiniteDone(length) { const self = this if(length === 0) { self.infiniteState = false } this.showLoad = false self.infiniteLoading = false }, onScroll() { if (this.onInfinite) { let scrollTop = this.getScrollTop() let scrollHeight = this.getScrollHeight() let windowHeight = this.getWindowHeight() if (scrollTop + windowHeight === scrollHeight) { this.showLoad = true this.infinite() } } }, // 滾動條在Y軸上的滾動距離 getScrollTop() { var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0 if (document.body) { bodyScrollTop = document.body.scrollTop } if (document.documentElement) { documentScrollTop = document.documentElement.scrollTop } scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop return scrollTop }, // 文檔的總高度 getScrollHeight() { var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0 if (document.body) { bodyScrollHeight = document.body.scrollHeight; } if (document.documentElement) { documentScrollHeight = document.documentElement.scrollHeight; } scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight return scrollHeight }, // 瀏覽器視口的高度 getWindowHeight() { var windowHeight = 0 if (document.compatMode === 'CSS1Compat') { windowHeight = document.documentElement.clientHeight } else { windowHeight = document.body.clientHeight } return windowHeight } } } </script> <style lang="scss" scoped> .mu-load-more { position: relative; overflow: hidden; } .mu-refresh-control { display: flex; margin: 0 auto; width: 80px; height: 80px; color: #2196f3; align-items: center; justify-content: center; background-color: #FFF; border-radius: 50%; -webkit-box-shadow: 0 3px 1px -2px rgba(0,0,0,.2), 0 2px 2px 0 rgba(0,0,0,.14), 0 1px 5px 0 rgba(0,0,0,.12); box-shadow: 0 3px 1px -2px rgba(0,0,0,.2), 0 2px 2px 0 rgba(0,0,0,.14), 0 1px 5px 0 rgba(0,0,0,.12); position: absolute; left: 50%; margin-left: -38px; margin-top: 24px; z-index: 90; } .mu-refresh-svg-icon { display: inline-block; width: 20px; height: 20px; fill: currentColor; } .refresh { -webkit-transform: rotate(360deg); animation: rotation 1s linear infinite; -moz-animation: rotation 1s linear infinite; -webkit-animation: rotation 1s linear infinite; -o-animation: rotation 1s linear infinite; } @-webkit-keyframes rotation { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .son { position: absolute; animation: lightAni 1s linear 1; } @keyframes lightAni { 0% { transform: translateY(0); } 50% { transform: translateY(-50px); } 100% { transform: translateY(-100px); } } </style>
應用組件
<scrollRefresh :on-refresh="refresh" :on-infinite="load"> <!-- 頁面內容 --> </scrollRefresh>
<script> // 引入組件 import scrollRefresh from '@/components/scrollRefresh' export default { components: { scrollRefresh } } </script>
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。