≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

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

人气:749 时间:2019-04-16

这篇文章主要为大家详细介绍了如何封装了一个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

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

本文来自:http://www.q1010.com/184/7074-0.html

注:关于如何封装了一个vue移动端下拉加载下一页数据的组件的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • 基于vue.js组件实现分页效果
  • Vue2.x Todo之自定义指令实现自动聚焦的方法
  • Vue实现简单分页器
  • 分析@Vue/Cli 3 Invalid Host header 错误解决办法
  • 分析VUE里子组件如何获取父组件动态变化的值
  • axios携带cookie配置分析(axios+koa)
  • Element输入框带历史查询记录的实现示例
  • 分析vue 兼容IE报错解决方案
  • 分析Vue.js v-for不支持IE9的解决方法
  • 如何在Vue.js中实现标签页组件分析
上一篇:分析Vue基于vue-quill-editor富文本编辑器使用心得
下一篇:分析@Vue/Cli 3 Invalid Host header 错误解决办法
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。