背景

  之前写过一篇《纯JS实现加载更多(VUE框架)》,它的逻辑思路比较清晰易懂,而今天看了一天公司项目的部分功能代码,发现同事们写的加载更多的功能更加的有趣,而且易于封装到一个组件当中,便于日后的重用。

思路

  同样是检测滚动,然后计算可滑动高度、滑动高度、屏幕高度然后计算什么时候可以向后台请求数据,加载数据。而不同的是这次结合了vue.js中的全局API之Vue.directive(自定义指令,关于它的知识,官网上面已经写得很清晰了),创建一个自定义指令,类似于v-show这样的指令,然后直接挂到DOM那里,再写上参数,通过directive对应的钩子函数(注意,这里的全局api也有自己相对应的钩子函数哟),来为绑定了该自定义指令的DOM节点添加滑动事件。

动手吧

  首先使用全局api  Vue.directive自定义自己的指令,取名为‘loadMore’,然后写下钩子函数inserted()、unbind()。

  其中onScroll()函数是自定义方法,作用是实现滚动加载更多的功能。

  inseted()钩子函数的作用是当被绑定元素插入父节点时,调用此钩子函数,为被绑定元素绑定监听滑动scroll的时间方法,一旦被绑定元素被监听到滑动,调用onScroll()函数实现加载更多。

  unbind()钩子函数的作用是当指令与元素解绑时,调用该钩子(即退出页面或者程序时调用)

  程序如下所示:

import Vue from 'vue';
Vue.directive('loadMore', {
inserted(ele, data) {
ele.addEventListener('scroll', data.def.onScroll.bind(null, data))
console.log(data)
console.log('inserted钩子函数被触发咯~~~')
},
unbind(ele, data) {
ele.removeEventListener('scroll', data).def.onScroll.bind(null, data)
console.log('解绑时调用!!!!')
},
onScroll(data, event) {
let target = event.target;
let loadMoreConfig = data.value;
let screenHeight = target.offsetHeight;
let scrollTop = target.scrollTop;
let totalHeight = target.scrollHeight;
if (!loadMoreConfig.loading && totalHeight > screenHeight && totalHeight - scrollTop - screenHeight < loadMoreConfig.scrollBottomOffset) {
loadMoreConfig.onScrollBottom();
loadMoreConfig.loading = true;
}
},
});

上面这段程序写在<script></script>里面,写在export default外面。

使用自定义指令

  因为上面定义了一个指令,叫loadMore, 像v-show、v-if一样挂载DOM中,然后传需要的参数就可以了,如下

<pull-refresh :next="reloadNewKnowLedgeDataList" class="knowledge-list-container" v-load-more="newPageInfo.loadMoreConfig">……

  上面传的参数是newPageInfo.loadMoreConfig,在data()里面,它是这样的

data() {
return {
newPageInfo: {
isLoadedData: false,
pageNo: 1, // 第一页
pageSize: 15, // 每一次加载的最大页数
dataList: [],
loadMoreConfig: {
onScrollBottom: this.onScrollBottomForNewPage, //调用加载的方法
scrollBottomOffset: 20, //距离底部门槛阈值
loading: false //是否加载
}
}
}

  看到了吗,因为之前在全局api directive里面自己定义的监听滑动后触发函数onScroll(),一旦触发它,函数就会调用this.onScrollBottomForNewPage()方法来加载数据

接着上面按逻辑执行

onScrollBottomForNewPage() {
this.newPageInfo.pageNo += 1;
this.queryNewKnowLedgeDataList();
}, queryNewKnowLedgeDataList(callback) {
this.loading(true);
this.queryKnowLedgeDataMethod('new').then((res) => {
if (callback && typeof callback === 'function') {
callback();
}
this.newPageInfo.isLoadedData = true;
this.loading(false);
this.processNewKnowLedgeData(res);
}, (error) => {
if (callback && typeof callback === 'function') {
callback();
}
this.loading(false);
this.newPageInfo.loadMoreConfig.loading = true;
this.newPageInfo.isLoadedData = true;
if (error !== null) {
this.$vux.toast.show({ text: '数据加载失败', type: 'text' });
}
});
}, queryKnowLedgeDataMethod(type) {
let params;
if (type === 'new') {
params = {
'pageNo': this.newPageInfo.pageNo,
'pageSize': this.newPageInfo.pageSize,
'findType': 0
};
} else {
params = {
'pageNo': this.hotPageInfo.pageNo,
'pageSize': this.hotPageInfo.pageSize,
'findType': 1
};
}
return this.$http.get('rule/findRuleNewOrHotList', { 'params': params });
}, processNewKnowLedgeData(res) {
if (!res) {
return;
}
if (res.data && res.data.length > 0) {
if (this.newPageInfo.pageNo === 1) {
this.newPageInfo.dataList = res.data;
} else {
this.newPageInfo.dataList = this.newPageInfo.dataList.concat(res.data);
}
this.newPageInfo.loadMoreConfig.loading = false;
} else {
this.newPageInfo.loadMoreConfig.loading = true;
if (this.newPageInfo.pageNo === 1) {
this.newPageInfo.dataList = [];
} else {
this.$vux.toast.show({ text: '没有更多', type: 'text' });
}
}
},

  

使用vue之directive设计列表加载更多的更多相关文章

  1. 微信小程序列表加载更多

    概述 基于小程序开发的列表加载更多例子. 详细 代码下载:http://www.demodashi.com/demo/13632.html 一.前言 基于小程序开发的列表加载更多例子. 二.运行效果 ...

  2. vue移动端上拉加载更多

    LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...

  3. vue项目上滑滚动加载更多&下拉刷新

    上滑滚动时获取内容高度.屏幕高度和滚动高度(此处#sslist要为内容是id) 内容高度  let innerHeight = document.querySelector("#sslist ...

  4. 基于jQuery实现点击列表加载更多效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>   < ...

  5. jQuery+php+Ajax文章列表点击加载更多功能

    jQuery+php+Ajax实现的一个简单实用的文章列表点击加载更多功能,点击加载更多按钮,文章列表加载更多数据,加载中有loading动画效果. js部分: <script type=&qu ...

  6. 基于 Vue.js 的移动端组件库mint-ui实现无限滚动加载更多

    通过多次爬坑,发现了这些监听滚动来加载更多的组件的共同点, 因为这些加载更多的方法是绑定在需要加载更多的内容的元素上的, 所以是进入页面则直接触发一次,当监听到滚动事件之后,继续加载更多, 所以对于无 ...

  7. 【Vue.js】加载更多—vue-infinite-scroll

    引言 今天用到了一个加载更多的插件,用起来很方便,插件的名字叫做vue-infinite-scroll 我们可以去npmjs.com官网看一下这个vue-infinite-scroll的用法,官网上面 ...

  8. 【转载】Vue自定义指令实现pc端加载更多

    转载来源:https://www.86886.wang/detail/5a6f19e644f9da55274c3bbd,谢谢作者分享! 原理 document.documentElement.scro ...

  9. vue 原生添加滚动加载更多

    vue中添加滚动加载更多,因为是单页面所以需要在跳出页面时候销毁滚动,要不会出现错乱.我们在mounted建立滚动,destroyed销毁滚动. mounted () { window.addEven ...

随机推荐

  1. C#项目学习记录

    1,   Visual Studio Code 添加VS 2017的开发人员命令提示符---C#编译环境 2,  C#编译器和CLI的安装 注意:自己的电脑上配置环境变量时,配置在系统变量的Path中 ...

  2. Oracle授权

    给连接权限 grant connect to 用户; 给资源权限 grant resource to 用户; 给DBA权限 grant dba to 用户; 授权语句 --select * from ...

  3. navicat连接mysql出现2059错误

    最近在学习django的时候需要用到数据库,于是便下载了navicat准备和mysql配套使用,但是在连接的时候确出现了如下问题: 网上查询过后,发现这个错误出现的原因是在mysql8之前的版本中加密 ...

  4. Sudoku(POJ2676/3074)

    Sudoku is one of the metaphysical techniques. If you understand the essence of it, you will have the ...

  5. ext__给grid Panel设置绑定事件

    使用面板来展示详情信息 1.创建一个面板 (双击添加) 2.给该面板设置itemid的值为:detailPanel 3.给面板设置模板 4.添加下面的内容 id:{id}</br> nam ...

  6. asp.net项目配置Web.config,支持JSON

    VS2013新建的web项目不支持直接下载json文件,可以在项目的web.config下插入如下的配置信息. <configuration> <system.web> < ...

  7. IDEA安装插件提示was not installed: Cannot download解决办法

    打开settings->system settings->updata,把下面的Use secure Connetion去掉

  8. centos7下zabbix记录

    Zabbixrpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm - ...

  9. Monad Explained in One Picture

    The point of Monad is composability. In the green category, T -> Monad<U> and U -> Monad ...

  10. 基于TensorFlow的深度学习系列教程 1——Hello World!

    最近看到一份不错的深度学习资源--Stanford中的CS20SI:<TensorFlow for Deep Learning Research>,正好跟着学习一下TensorFlow的基 ...