如何封裝了一個vue移動端下拉加載下一頁數(shù)據(jù)的組件
前言
簡單封裝了一個vue下拉加載組件,分享一下,已放到github和前端資源庫,歡迎下載!
組件代碼
<template>
<div class="my-scroll" :class="[scrollState?'prohibit':'allow']" ref="myScroll" @scroll.passive="onScroll($event)" @touchmove="onScroll($event)" >
<!-- top -->
<div class="scroll-list">
<slot name='scrollList'></slot>
<div class="scroll-bottom">
<div v-if="state==1">
<i><img :src="Load"/></i>
<p>加載中</p>
</div>
<div v-if="state==2">加載完成</div>
<div v-if="state==3">沒有數(shù)據(jù)了</div>
</div>
</div>
</div>
</template>
<script type="text/javascript">
import Load from '@/assets/Load.gif'
export default {
name: 'myScroll',
props: {
'onPull': { // 加載回調(diào)
type: Function,
require: true
},
'scrollState': {// 是否可滑動
type: Boolean,
require: true
},
loaded: {
type: Boolean,
default() {
return false
}
}
},
data() {
return {
Load,
timeoutId: null,
state: 0,
myScroll: null
}
},
methods: {
/*
* 加載中:1
* 加載完成:2
* 沒有更多:3
*/
setState(index) { // 修改狀態(tài)
this.state = index
// console.log(this.state)
},
onScroll(e) {
const _this = this
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
// console.log(window.innerHeight + scrollTop, this.myScroll.offsetHeight)
if (!this.loaded && window.innerHeight + scrollTop - 50 >= this.myScroll.offsetHeight) {
clearTimeout(this.timeoutId)
_this.timeoutId = setTimeout(() => {
_this.bottomCallback()
}, 100)
}
},
bottomCallback() { // 加載回調(diào)
// console.log('回調(diào)', new Date().getTime())
if (this.state != 3) {
this.state = 1
this.onPull()
}
}
},
mounted() {
this.myScroll = this.$refs.myScroll // 獲取滑條dom
}
}
</script>
<style lang="scss" scoped>
.allow{
overflow:hidden;
height: auto;
}
.prohibit{
max-width: 100%;
max-height: 100%;
height: 100%;
overflow:hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
will-change: transform;
transition: all 450ms;
backface-visibility: hidden;
perspective: 1000;
}
.my-scroll{
position: relative;
color: #999;
.scroll-top{
text-align: center;
display:flex;
position:absolute;
top:0;
left:0;
width:100%;
overflow: hidden;
}
.scroll-list{
overflow:hidden;
min-height: 100%;
}
.scroll-bottom{
text-align: center;
line-height: .8rem;
div{
display:flex;
height:auto;
width:100%;
justify-content: center;
align-items:center;
flex-wrap: wrap;
i{
flex:1 0 100%;
display:block;
height: 0.4rem;
}
img{
width:0.6rem;
}
p{
flex:1 0 100%;
}
}
}
}
</style>
使用
<template>
<div id="app">
<my-scroll class="scrolls" ref="myScroll" :on-pull="getList" :loaded="loaded" :scroll-state="scrollState">
<div slot="scrollList">
<div class="list" v-for="(item,index) in listData" :key="index">{{item.name}}</div>
</div>
</my-scroll>
</div>
</template>
<script>
import myScroll from "./components/vue-scroll.vue";
import axios from 'axios'
export default {
name: "app",
data(){
return{
scrollState: true, // 是否可以滑動
loaded: false,
iPage: 1,
listData:[],
iPageSize: 10,
}
},
methods: {
getList(){
this.$refs.myScroll.setState(1)
let _this = this
// ajax 請求
axios.get('https://easy-mock.com/mock/5b90f971ce624c454133ee2d/scoll/datalist').then(function (response) {
if (response.data.code == 200 && response.data.data.pagelist.length > 0 && !_this.loaded) {
if (_this.iPage == 1) {
_this.listData = response.data.data.pagelist
} else {
_this.listData.push(...response.data.data.pagelist)
}
_this.iPage++
_this.$refs.myScroll.setState(2)
} else {
if (_this.iPage == 1) {
_this.czListData = []
}
_this.loaded = true
_this.$refs.myScroll.setState(3)
}
})
.catch(function (error) {
console.log(error);
});
}
},
mounted () {
this.getList()
},
components: {
myScroll
}
};
</script>
<style scoped>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.scrolls{
font-size:.24rem;
}
.list{
height:.9rem;
line-height: .9rem;
margin-bottom:.1rem;
border-bottom:1px solid #dedede;
color:#999;
font-size:.28rem;
}
</style>
組件已放到github,歡迎下載和star
可以直接在vue項(xiàng)目中運(yùn)行這個組件
github地址:https://github.com/confidence68/vue_mobile_scrollLoadpage
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue?select?change事件如何傳遞自定義參數(shù)
這篇文章主要介紹了vue?select?change事件如何傳遞自定義參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue項(xiàng)目配置sass及引入外部scss文件方式
這篇文章主要介紹了vue項(xiàng)目配置sass及引入外部scss文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3實(shí)現(xiàn)父組件提交校驗(yàn)多個子組件
這篇文章主要為大家詳細(xì)介紹了vue3如何實(shí)現(xiàn)父組件在提交事件中校驗(yàn)多個子組件中的form件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2023-11-11
Vue3使用ResizeObserver監(jiān)聽元素的尺寸寬度變化
要監(jiān)聽 div 寬度的變化,可以使用 ResizeObserver 接口,ResizeObserver 允許你觀察一個或多個元素的尺寸變化,并在發(fā)生變化時執(zhí)行回調(diào)函數(shù),所以本文給大家介紹了Vue3如何使用ResizeObserver監(jiān)聽元素的尺寸寬度變化,需要的朋友可以參考下2024-08-08
element組件el-date-picker禁用當(dāng)前時分秒之前的日期時間選擇
本文主要介紹了element組件el-date-picker禁用當(dāng)前時分秒之前的日期時間選擇,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Vue3-KeepAlive,多個頁面使用keepalive方式
這篇文章主要介紹了Vue3-KeepAlive,多個頁面使用keepalive方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

