如何封装了一个vue移动端下拉加载下一页数据的组件

 更新时间:2019年01月06日 17:09:08   作者:Haorooms  
这篇文章主要介绍了如何封装了一个vue移动端下拉加载下一页数据的组件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

简单封装了一个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">没有数据了</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>

组件已放到github,欢迎下载和star

可以直接在vue项目中运行这个组件

github地址:https://github.com/confidence68/vue_mobile_scrollLoadpage

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vuex的核心概念和基本使用详解

    vuex的核心概念和基本使用详解

    这篇文章主要为大家介绍了vuex的核心概念和基本使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • Element中el-table动态合并单元格(span-method方法)

    Element中el-table动态合并单元格(span-method方法)

    本文主要介绍了Element中el-table动态合并单元格(span-method方法),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • vue获取el-form的整体验证状态

    vue获取el-form的整体验证状态

    本文主要介绍了vue获取el-form的整体验证状态,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • vue下使用nginx刷新页面404的问题解决

    vue下使用nginx刷新页面404的问题解决

    这篇文章主要介绍了vue下使用nginx刷新页面404的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • VueJs路由跳转——vue-router的使用详解

    VueJs路由跳转——vue-router的使用详解

    本篇文章主要介绍了VueJs路由跳转——vue-router的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • vue项目基于WebRTC实现一对一音视频通话

    vue项目基于WebRTC实现一对一音视频通话

    这篇文章主要介绍了vue项目基于WebRTC实现一对一音视频通话效果,实现代码分为前端和后端两部分代码,需要的朋友可以参考下
    2024-05-05
  • vue3.x源码剖析之数据响应式的深入讲解

    vue3.x源码剖析之数据响应式的深入讲解

    这篇文章主要给大家介绍了关于vue3.x源码剖析之数据响应式的相关资料,在讲解过程中,我们会对比Vue2.x的API特性,使用有哪些区别,需要的朋友可以参考下
    2022-01-01
  • 前端Vue页面中展示本地图片简单代码示例

    前端Vue页面中展示本地图片简单代码示例

    今天遇到一个在vue文件中引入本地图片的问题,于是有了这篇文章,本文主要给大家介绍了关于前端Vue页面中展示本地图片的相关资料,需要的朋友可以参考下
    2023-12-12
  • 如何使用Vue3+Vite+TS快速搭建一套实用的脚手架

    如何使用Vue3+Vite+TS快速搭建一套实用的脚手架

    Vite是一个面向现代浏览器的一个更轻、更快的 Web 应用开发工具,下面这篇文章主要给大家介绍了关于如何使用Vue3+Vite+TS快速搭建一套实用脚手架的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • python虚拟环境 virtualenv的简单使用

    python虚拟环境 virtualenv的简单使用

    virtualenv是一个创建隔绝的Python环境的工具。这篇文章主要介绍了python虚拟环境 virtualenv的简单使用,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01

最新评论