您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下怎么封装了一个vue移动端下拉加载下一页数据的组件,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
组件代码
<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">没有数据了</div> </div> </div> </div> </template> <script type="text/javascript"> import Load from '@/assets/Load.gif' export default { name: 'myScroll', props: { 'onPull': { // 加载回调 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) { // 修改状态 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() { // 加载回调 // console.log('回调', 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>
看完了这篇文章,相信你对“怎么封装了一个vue移动端下拉加载下一页数据的组件”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。